Struts2 文件上传和下载
首先我们写一个单文件长传的fileupload.jsp:
1 <body> 2 <s:fielderror></s:fielderror> <!-- 报错信息 --> 3 <s:form action="upload" method="post" enctype="multipart/form-data">//以二进制格式格式 4 文件:<s:file label="文件上传" name="java1234"></s:file> //文件 5 <s:submit value="提交"></s:submit> 6 </s:form> 7 </body>
写一个单文件上传FileuploadAction:
1 public class FileuploadAction extends ActionSupport{ 2 private static final long serialVersionUID = 1L; 3 private File java1234;//文件 4 private String java1234FileName;//文件名称 5 private String java1234ContentType;//文件类型 6 public File getJava1234() { 7 return java1234; 8 } 9 public void setJava1234(File java1234) { 10 this.java1234 = java1234; 11 } 12 public String getJava1234FileName() { 13 return java1234FileName; 14 } 15 public void setJava1234FileName(String java1234FileName) { 16 this.java1234FileName = java1234FileName; 17 } 18 public String getJava1234ContentType() { 19 return java1234ContentType; 20 } 21 public void setJava1234ContentType(String java1234ContentType) { 22 this.java1234ContentType = java1234ContentType; 23 } 24 @Override 25 public String execute() throws Exception { 26 System.out.println("文件名称:"+java1234FileName); 27 System.out.println("文件类型"+java1234ContentType); 28 File destFile=new File("D:/"+java1234FileName); 29 FileUtils.copyFile(java1234, destFile); 30 return SUCCESS; 31 } 32 }
配置struts.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 <struts> 6 <constant name="struts.multipart.maxSize" value="520000000"></constant>//文件的最大为520M 7 <package name="manager" extends="struts-default"> 8 <action name="upload" class="com.java1234.action.FileuploadAction"> 9 <result name="input">/fileupload.jsp</result> 10 <result name="success">/success.jsp</result> 11 <!-- <interceptor-ref name="fileUpload">可以设置拦截器对长传的文件进行拦截处理。 12 <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg,audio/mp3</param> 13 <param name="maximumSize">15728640</param>15MB 14 </interceptor-ref> 15 <interceptor-ref name="defaultStack"></interceptor-ref> --> 16 </action> 17 </package> 18 </struts>
效果:
多文件上传:
filesupload.Jsp
1 <body> 2 <s:fielderror></s:fielderror> <!-- 报错信息 --> 3 <s:form action="uploads" method="post" enctype="multipart/form-data"> 4 <s:file label="文件上传1" name="java1234"></s:file> 5 <s:file label="文件上传2" name="java1234"></s:file> 6 <s:file label="文件上传3" name="java1234"></s:file> 7 <s:submit value="提交"></s:submit> 8 </s:form> 9 </body>
FilesUploadAction代码:
1 public class FilesuploadAction extends ActionSupport{ 2 private static final long serialVersionUID = 1L; 3 private File[] java1234;//文件 4 private String[] java1234FileName;//文件名称 5 private String[] java1234ContentType;//文件类型 6 public File[] getJava1234() { 7 return java1234; 8 } 9 public void setJava1234(File[] java1234) { 10 this.java1234 = java1234; 11 } 12 13 public String[] getJava1234FileName() { 14 return java1234FileName; 15 } 16 17 public void setJava1234FileName(String[] java1234FileName) { 18 this.java1234FileName = java1234FileName; 19 } 20 public String[] getJava1234ContentType() { 21 return java1234ContentType; 22 } 23 public void setJava1234ContentType(String[] java1234ContentType) { 24 this.java1234ContentType = java1234ContentType; 25 } 26 @Override 27 public String execute() throws Exception { 28 for(int i=0;i<java1234.length;i++){ 29 System.out.println("文件名称:"+java1234FileName[i]); 30 System.out.println("文件类型"+java1234ContentType[i]); 31 File destFile=new File("D:/"+java1234FileName[i]); 32 FileUtils.copyFile(java1234[i], destFile); 33 } 34 return SUCCESS; 35 } 36 }
使用数组进行处理。
下载文件jsp:
1 <body> 2 <a href="download">文件下载</a> 3 </body>
FiledownloadAction代码:
1 public class FiledownloadAction extends ActionSupport{ 2 private static final long serialVersionUID = 1L; 3 private String fileName; 4 public String getFileName() throws Exception{ 5 fileName=new String(fileName.getBytes(),"IOS8859-1"); 6 return fileName; 7 } 8 public void setFileName(String fileName) { 9 this.fileName = fileName; 10 } 11 public InputStream getInputStream()throws Exception{ 12 File newfile=new File("D:/文件名.jpg"); 13 this.fileName="文件名"; 14 return new FileInputStream(newfile); 15 } 16 }
Struts.xml文件配置:
1 <struts> 2 <constant name="struts.multipart.maxSize" value="520000000"></constant> 3 <package name="manager" extends="struts-default"> 4 <action name="uploads" class="com.java1234.action.FilesuploadAction"> 5 <result name="input">/filesupload.jsp</result> 6 <result name="success">/success.jsp</result> 7 </action> 8 <action name="download" class="com.java1234.action.FiledownloadAction"> 9 <result type="stream"> 10 <param name="contentDisposition">attachment;filename=${fileName}</param> 11 </result> 12 </action> 13 </package> 14 </struts>