本文介绍了:
1.基于表单的文件上传
2.Struts 2 的文件下载
3.Struts2.文件上传
4.使用FileInputStream FileOutputStream文件流来上传
5.使用FileUtil上传
6.使用IOUtil上传
7.使用IOUtil上传
8.使用数组上传多个文件
9.使用List上传多个文件
----1.基于表单的文件上传-----
fileupload.jsp
- <body>
- <form action="showFile.jsp" name="myForm" method="post" enctype="multipart/form-data">
- 选择上传的文件
- <input type="file" name="myfile"><br/><br/>
- <input type="submit" name="mySubmit" value="上传"/>
- </form>
- </body>
<body> <form action="showFile.jsp" name="myForm" method="post" enctype="multipart/form-data"> 选择上传的文件 <input type="file" name="myfile"><br/><br/> <input type="submit" name="mySubmit" value="上传"/> </form> </body>
showFile.jsp
- <body>
- 上传的文件的内容如下:
- <%
- InputStream is=request.getInputStream();
- InputStreamReader isr=new InputStreamReader(is);
- BufferedReader br=new BufferedReader(isr);
- String content=null;
- while((content=br.readLine())!=null){
- out.print(content+"<br/>");
- }
- %>
- </body>
<body> 上传的文件的内容如下: <% InputStream is=request.getInputStream(); InputStreamReader isr=new InputStreamReader(is); BufferedReader br=new BufferedReader(isr); String content=null; while((content=br.readLine())!=null){ out.print(content+"<br/>"); } %> </body>
----2.手动上传-----
- 通过二进制刘获取上传文件的内容,并将上传的文件内容保存到服务器的某个目录,这样就实现了文件上传。由于这个处理过程完全依赖与开发自己处理二进制流,所以也称为“手动上传”。
- 从上面的第一个例子可以看到,使用二进制流获取的上传文件的内容与实际文件的内容有还是有一定的区别,包含了很多实际文本中没有的字符。所以需要对获取的内容进行解析,去掉额外的字符。
通过二进制刘获取上传文件的内容,并将上传的文件内容保存到服务器的某个目录,这样就实现了文件上传。由于这个处理过程完全依赖与开发自己处理二进制流,所以也称为“手动上传”。 从上面的第一个例子可以看到,使用二进制流获取的上传文件的内容与实际文件的内容有还是有一定的区别,包含了很多实际文本中没有的字符。所以需要对获取的内容进行解析,去掉额外的字符。
----3 Struts2.文件上传----
- Struts2中使用Common-fileUpload文件上传框架,需要在web应用中增加两个Jar 文件, 即 commons-fileupload.jar. commons-io.jar
- 需要使用fileUpload拦截器:具体的说明在 struts2-core-2.3.4.jar \org.apache.struts2.interceptor\FileUploadInterceptor.class 里面
- 下面来看看一点源代码
Struts2中使用Common-fileUpload文件上传框架,需要在web应用中增加两个Jar 文件, 即 commons-fileupload.jar. commons-io.jar 需要使用fileUpload拦截器:具体的说明在 struts2-core-2.3.4.jar \org.apache.struts2.interceptor\FileUploadInterceptor.class 里面 下面来看看一点源代码
- public class FileUploadInterceptor extends AbstractInterceptor {
- private static final long serialVersionUID = -4764627478894962478L;
- protected static final Logger LOG = LoggerFactory.getLogger(FileUploadInterceptor.class);
- private static final String DEFAULT_MESSAGE = "no.message.found";
- protected boolean useActionMessageBundle;
- protected Long maximumSize;
- protected Set<String> allowedTypesSet = Collections.emptySet();
- protected Set<String> allowedExtensionsSet = Collections.emptySet();
- private PatternMatcher matcher;
- @Inject
- public void setMatcher(PatternMatcher matcher) {
- this.matcher = matcher;
- }
- public void setUseActionMessageBundle(String value) {
- this.useActionMessageBundle = Boolean.valueOf(value);
- }
- //这就是struts.xml 中param为什么要配置为 allowedExtensions
- public void setAllowedExtensions(String allowedExtensions) {
- allowedExtensionsSet = TextParseUtil.commaDelimitedStringToSet(allowedExtensions);
- }
- //这就是struts.xml 中param为什么要配置为 allowedTypes 而不是 上面的allowedTypesSet
- public void setAllowedTypes(String allowedTypes) {
- allowedTypesSet = TextParseUtil.commaDelimitedStringToSet(allowedTypes);
- }
- public void setMaximumSize(Long maximumSize) {
- this.maximumSize = maximumSize;
- }
- }
public class FileUploadInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -4764627478894962478L; protected static final Logger LOG = LoggerFactory.getLogger(FileUploadInterceptor.class); private static final String DEFAULT_MESSAGE = "no.message.found"; protected boolean useActionMessageBundle; protected Long maximumSize; protected Set<String> allowedTypesSet = Collections.emptySet(); protected Set<String> allowedExtensionsSet = Collections.emptySet(); private PatternMatcher matcher; @Inject public void setMatcher(PatternMatcher matcher) { this.matcher = matcher; } public void setUseActionMessageBundle(String value) { this.useActionMessageBundle = Boolean.valueOf(value); } //这就是struts.xml 中param为什么要配置为 allowedExtensions public void setAllowedExtensions(String allowedExtensions) { allowedExtensionsSet = TextParseUtil.commaDelimitedStringToSet(allowedExtensions); } //这就是struts.xml 中param为什么要配置为 allowedTypes 而不是 上面的allowedTypesSet public void setAllowedTypes(String allowedTypes) { allowedTypesSet = TextParseUtil.commaDelimitedStringToSet(allowedTypes); } public void setMaximumSize(Long maximumSize) { this.maximumSize = maximumSize; } }
- 官员文件初始值大小 上面的类中的说明
- <li>maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set
- * on the action. Note, this is <b>not</b> related to the various properties found in struts.properties.
- * Default to approximately 2MB.</li>
- 具体说的是这个值在struts.properties 中有设置。 下面就来看 里面的设置
- ### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data
- 文件上传解析器
- # struts.multipart.parser=cos
- # struts.multipart.parser=pell
- #默认 使用jakata框架上传文件
- struts.multipart.parser=jakarta
- #上传时候 默认的临时文件目录
- # uses javax.servlet.context.tempdir by default
- struts.multipart.saveDir=
- #上传时候默认的大小
- struts.multipart.maxSize=2097152
官员文件初始值大小 上面的类中的说明 <li>maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set * on the action. Note, this is <b>not</b> related to the various properties found in struts.properties. * Default to approximately 2MB.</li> 具体说的是这个值在struts.properties 中有设置。 下面就来看 里面的设置 ### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data 文件上传解析器 # struts.multipart.parser=cos # struts.multipart.parser=pell #默认 使用jakata框架上传文件 struts.multipart.parser=jakarta #上传时候 默认的临时文件目录 # uses javax.servlet.context.tempdir by default struts.multipart.saveDir= #上传时候默认的大小 struts.multipart.maxSize=2097152
案例:使用FileInputStream FileOutputStream文件流来上传
action.java
- package com.sh.action;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class MyUpAction extends ActionSupport {
- private File upload; //上传的文件
- private String uploadContentType; //文件的类型
- private String uploadFileName; //文件名称
- private String savePath; //文件上传的路径
- //注意这里的保存路径
- public String getSavePath() {
- return ServletActionContext.getRequest().getRealPath(savePath);
- }
- public void setSavePath(String savePath) {
- this.savePath = savePath;
- }
- @Override
- public String execute() throws Exception {
- System.out.println("type:"+this.uploadContentType);
- String fileName=getSavePath()+"\\"+getUploadFileName();
- FileOutputStream fos=new FileOutputStream(fileName);
- FileInputStream fis=new FileInputStream(getUpload());
- byte[] b=new byte[1024];
- int len=0;
- while ((len=fis.read(b))>0) {
- fos.write(b,0,len);
- }
- fos.flush();
- fos.close();
- fis.close();
- return SUCCESS;
- }
- //get set
- }
package com.sh.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class MyUpAction extends ActionSupport { private File upload; //上传的文件 private String uploadContentType; //文件的类型 private String uploadFileName; //文件名称 private String savePath; //文件上传的路径 //注意这里的保存路径 public String getSavePath() { return ServletActionContext.getRequest().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } @Override public String execute() throws Exception { System.out.println("type:"+this.uploadContentType); String fileName=getSavePath()+"\\"+getUploadFileName(); FileOutputStream fos=new FileOutputStream(fileName); FileInputStream fis=new FileInputStream(getUpload()); byte[] b=new byte[1024]; int len=0; while ((len=fis.read(b))>0) { fos.write(b,0,len); } fos.flush(); fos.close(); fis.close(); return SUCCESS; } //get set }
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <constant name="struts.i18n.encoding" value="utf-8"/>
- <constant name="struts.devMode" value="true"/>
- <constant name="struts.convention.classes.reload" value="true" />
- <constant name="struts.multipart.saveDir" value="f:/tmp"/>
- <package name="/user" extends="struts-default">
- <action name="up" class="com.sh.action.MyUpAction">
- <result name="input">/up.jsp</result>
- <result name="success">/success.jsp</result>
- <!-- 在web-root目录下新建的一个 upload目录 用于保存上传的文件 -->
- <param name="savePath">/upload</param>
- <interceptor-ref name="fileUpload">
- <!--采用设置文件的类型 来限制上传文件的类型-->
- <param name="allowedTypes">text/plain</param>
- <!--采用设置文件的后缀来限制上传文件的类型 -->
- <param name="allowedExtensions">png,txt</param>
- <!--设置文件的大小 默认为 2M [单位:byte] -->
- <param name="maximumSize">1024000</param>
- </interceptor-ref>
- <interceptor-ref name="defaultStack"/>
- </action>
- </package>
- </struts>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.i18n.encoding" value="utf-8"/> <constant name="struts.devMode" value="true"/> <constant name="struts.convention.classes.reload" value="true" /> <constant name="struts.multipart.saveDir" value="f:/tmp"/> <package name="/user" extends="struts-default"> <action name="up" class="com.sh.action.MyUpAction"> <result name="input">/up.jsp</result> <result name="success">/success.jsp</result> <!-- 在web-root目录下新建的一个 upload目录 用于保存上传的文件 --> <param name="savePath">/upload</param> <interceptor-ref name="fileUpload"> <!--采用设置文件的类型 来限制上传文件的类型--> <param name="allowedTypes">text/plain</param> <!--采用设置文件的后缀来限制上传文件的类型 --> <param name="allowedExtensions">png,txt</param> <!--设置文件的大小 默认为 2M [单位:byte] --> <param name="maximumSize">1024000</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> </action> </package> </struts>
up.jsp
- <body>
- <h2>Struts2 上传文件</h2>
- <s:fielderror/>
- <s:form action="up" method="post" name="upform" id="form1" enctype="multipart/form-data" theme="simple">
- 选择文件:
- <s:file name="upload" cssStyle="width:300px;"/>
- <s:submit value="确定"/>
- </s:form>
- </body>
<body> <h2>Struts2 上传文件</h2> <s:fielderror/> <s:form action="up" method="post" name="upform" id="form1" enctype="multipart/form-data" theme="simple"> 选择文件: <s:file name="upload" cssStyle="width:300px;"/> <s:submit value="确定"/> </s:form> </body>
success.jsp
- <body>
- <b>上传成功!</b>
- <s:property value="uploadFileName"/><br/>
- [img]<s:property value="'upload/'+uploadFileName"/>[/img]
- </body>
<body> <b>上传成功!</b> <s:property value="uploadFileName"/><br/> [img]<s:property value="'upload/'+uploadFileName"/>[/img] </body>
案例:使用FileUtil上传
action.java
- package com.sh.action;
- import java.io.File;
- import java.io.IOException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Random;
- import java.util.UUID;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class FileUtilUpload extends ActionSupport {
- private File image; //文件
- private String imageFileName; //文件名
- private String imageContentType;//文件类型
- public String execute(){
- try {
- if(image!=null){
- //文件保存的父目录
- String realPath=ServletActionContext.getServletContext()
- .getRealPath("/image");
- //要保存的新的文件名称
- String targetFileName=generateFileName(imageFileName);
- //利用父子目录穿件文件目录
- File savefile=new File(new File(realPath),targetFileName);
- if(!savefile.getParentFile().exists()){
- savefile.getParentFile().mkdirs();
- }
- FileUtils.copyFile(image, savefile);
- ActionContext.getContext().put("message", "上传成功!");
- ActionContext.getContext().put("filePath", targetFileName);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return "success";
- }
- /**
- * new文件名= 时间 + 随机数
- * @param fileName: old文件名
- * @return new文件名
- */
- private String generateFileName(String fileName) {
- //时间
- DateFormat df = new SimpleDateFormat("yyMMddHHmmss");
- String formatDate = df.format(new Date());
- //随机数
- int random = new Random().nextInt(10000);
- //文件后缀
- int position = fileName.lastIndexOf(".");
- String extension = fileName.substring(position);
- return formatDate + random + extension;
- }
- //get set
- }
package com.sh.action; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import java.util.UUID; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUtilUpload extends ActionSupport { private File image; //文件 private String imageFileName; //文件名 private String imageContentType;//文件类型 public String execute(){ try { if(image!=null){ //文件保存的父目录 String realPath=ServletActionContext.getServletContext() .getRealPath("/image"); //要保存的新的文件名称 String targetFileName=generateFileName(imageFileName); //利用父子目录穿件文件目录 File savefile=new File(new File(realPath),targetFileName); if(!savefile.getParentFile().exists()){ savefile.getParentFile().mkdirs(); } FileUtils.copyFile(image, savefile); ActionContext.getContext().put("message", "上传成功!"); ActionContext.getContext().put("filePath", targetFileName); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "success"; } /** * new文件名= 时间 + 随机数 * @param fileName: old文件名 * @return new文件名 */ private String generateFileName(String fileName) { //时间 DateFormat df = new SimpleDateFormat("yyMMddHHmmss"); String formatDate = df.format(new Date()); //随机数 int random = new Random().nextInt(10000); //文件后缀 int position = fileName.lastIndexOf("."); String extension = fileName.substring(position); return formatDate + random + extension; } //get set }
struts.xml
- <action name="fileUtilUpload" class="com.sh.action.FileUtilUpload">
- <result name="input">/fileutilupload.jsp</result>
- <result name="success">/fuuSuccess.jsp</result>
- </action>
<action name="fileUtilUpload" class="com.sh.action.FileUtilUpload"> <result name="input">/fileutilupload.jsp</result> <result name="success">/fuuSuccess.jsp</result> </action>
fileutilupload.jsp
- <form action="${pageContext.request.contextPath }/fileUtilUpload.action"
- enctype="multipart/form-data" method="post">
- 文件:<input type="file" name="image"/>
- <input type="submit" value="上传"/>
- </form>
<form action="${pageContext.request.contextPath }/fileUtilUpload.action" enctype="multipart/form-data" method="post"> 文件:<input type="file" name="image"/> <input type="submit" value="上传"/> </form>
fuuSuccess.jsp
- body>
- <b>${message}</b>
- ${imageFileName}<br/>
- <img src="upload/${filePath}"/>
- </body>
body> <b>${message}</b> ${imageFileName}<br/> <img src="upload/${filePath}"/> </body>
案例:使用IOUtil上传
action.java
- package com.sh.action;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.UUID;
- import org.apache.commons.io.IOUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class IOUtilUpload extends ActionSupport {
- private File image; //文件
- private String imageFileName; //文件名
- private String imageContentType;//文件类型
- public String execute(){
- try {
- if(image!=null){
- //文件保存的父目录
- String realPath=ServletActionContext.getServletContext()
- .getRealPath("/image");
- //要保存的新的文件名称
- String targetFileName=generateFileName(imageFileName);
- //利用父子目录穿件文件目录
- File savefile=new File(new File(realPath),targetFileName);
- if(!savefile.getParentFile().exists()){
- savefile.getParentFile().mkdirs();
- }
- FileOutputStream fos=new FileOutputStream(savefile);
- FileInputStream fis=new FileInputStream(image);
- //如果复制文件的时候 出错了返回 值就是 -1 所以 初始化为 -2
- Long result=-2L; //大文件的上传
- int smresult=-2; //小文件的上传
- //如果文件大于 2GB
- if(image.length()>1024*2*1024){
- result=IOUtils.copyLarge(fis, fos);
- }else{
- smresult=IOUtils.copy(fis, fos);
- }
- if(result >-1 || smresult>-1){
- ActionContext.getContext().put("message", "上传成功!");
- }
- ActionContext.getContext().put("filePath", targetFileName);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return SUCCESS;
- }
- /**
- * new文件名= 时间 + 全球唯一编号
- * @param fileName old文件名
- * @return new文件名
- */
- private String generateFileName(String fileName) {
- //时间
- DateFormat df = new SimpleDateFormat("yy_MM_dd_HH_mm_ss");
- String formatDate = df.format(new Date());
- //全球唯一编号
- String uuid=UUID.randomUUID().toString();
- int position = fileName.lastIndexOf(".");
- String extension = fileName.substring(position);
- return formatDate + uuid + extension;
- }
- //get set
- }
package com.sh.action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class IOUtilUpload extends ActionSupport {
private File image; //文件
private String imageFileName; //文件名
private String imageContentType;//文件类型
public String execute(){
try {
if(image!=null){
//文件保存的父目录
String realPath=ServletActionContext.getServletContext()
.getRealPath("/image");
//要保存的新的文件名称
String targetFileName=generateFileName(imageFileName);
//利用父子目录穿件文件目录
File savef