struts2实现多文件上传的示例代码
开发环境JDK1.8 eclipse struts2-2.3.31
1.创建web项目
2.导入struts2核心jar包
3.更改web.xml配置文件(只要配置好struts2的Filter就好)
4.创建src/struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 --> <constant name="struts.action.extension" value="do" /> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 开发模式下使用,这样可以打印出更详细的错误信息 --> <constant name="struts.devMode" value="true" /> <!-- 默认的视图主题 --> <constant name="struts.ui.theme" value="simple" /> <!--<constant name="struts.objectFactory" value="spring" />--> <!--解决乱码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) --> <constant name="struts.multipart.maxSize" value="10701096"/> <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir --> <constant name="struts.multipart.saveDir " value="d:/tmp" /> <package name="upload" extends="struts-default"> <action name="fileUpload" class="com.ifan.action.FileUpload"> <!-- 动态设置savePath的属性值 --> <param name="savePath">WEB-INF/images</param> <result name="success">/success.jsp</result> <result name="input">/error.jsp</result> <interceptor-ref name="fileUpload"> <!-- 文件过滤 --> <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param> <!-- 文件大小, 以字节为单位 --> <param name="maximumSize">1025956</param> </interceptor-ref> <!-- 默认拦截器必须放在fileUpload之后,否则无效 --> <interceptor-ref name="defaultStack" /> </action> </package> </struts>
5.创建src/com.ifan.action.FileUpload.Java
package com.ifan.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUpload extends ActionSupport{ private File[] image; //上传的文件 private String[] imageFileName; //文件名称 private String[] imageContentType; //文件类型 public String execute() throws Exception { ServletActionContext.getRequest().setCharacterEncoding("UTF-8"); String realpath = ServletActionContext.getServletContext().getRealPath("/images"); System.out.println(realpath); if (image != null) { File savedir=new File(realpath); if(!savedir.getParentFile().exists()) savedir.getParentFile().mkdirs(); for(int i=0;i<image.length;i++){ File savefile = new File(savedir, imageFileName[i]); FileUtils.copyFile(image[i], savefile); } ActionContext.getContext().put("message", "文件上传成功"); } return "success"; } public File[] getImage() { return image; } public void setImage(File[] image) { this.image = image; } public String[] getImageContentType() { return imageContentType; } public void setImageContentType(String[] imageContentType) { this.imageContentType = imageContentType; } public String[] getImageFileName() { return imageFileName; } public void setImageFileName(String[] imageFileName) { this.imageFileName = imageFileName; } }
6.创建WebContent/index.jsp ,作为上传文件的页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <body> <!-- Struts2的文件上传标签 --> <s:form action="fileUpload" namespace="/" method="POST" enctype="multipart/form-data"> <!-- 该name需要和后台的File类型的名字对应起来,否则将得不到该文件 size 上传文件的大小 --> <s:file name="image" label="Select a File to upload" size="40" /> <s:file name="image" label="Select a File to upload" size="40" /> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
7.创建WebContent/success.jsp 作为文件上传成功跳转的页面,创建WebContent/error.jsp 作为文件上传失败的页面 , 创建WebContent/images文件夹,作为上传文件的存储位置
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
mybatisplus报Invalid bound statement (not found)错误的解决方法
搭建项目时使用了mybatisplus,项目能够正常启动,但在调用mapper方法查询数据库时报Invalid bound statement (not found)错误。本文给大家分享解决方案,感兴趣的朋友跟随小编一起看看吧2020-08-08jenkins+maven+svn自动部署和发布的详细图文教程
Jenkins是一个开源的、可扩展的持续集成、交付、部署的基于web界面的平台。这篇文章主要介绍了jenkins+maven+svn自动部署和发布的详细图文教程,需要的朋友可以参考下2020-09-09Java Swing CardLayout卡片布局的实现示例
这篇文章主要介绍了Java Swing CardLayout卡片布局的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-12-12shuffle的关键阶段sort(Map端和Reduce端)源码分析
今天小编就为大家分享一篇关于shuffle的关键阶段sort(Map端和Reduce端)源码分析,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2019-01-01Spring中BeanFactory与FactoryBean接口的区别详解
这篇文章主要给大家介绍了关于Spring中BeanFactory与FactoryBean接口的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-03-03
最新评论