实现原理
Struts 2是通过Commons FileUpload文件上传。
Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件
具体实现
1.单文件的上传(大文件的上传、上传文件的类型):
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:fielderror></s:fielderror><!-- 接收拦截器返回的错误信息 --> <form action="File_insert" method="post" enctype="multipart/form-data"> <input type="file" name="haha" /> <input type="submit" name="提交" /> </form> </body> </html>
struts.xml文件
<!-- struts2默认的上传文件大小是2M,超出时就要配置下面的这句话:大文件上传 --> <constant name="struts.multipart.maxSize" value="20000000"></constant> <!-- file文件上传,文件上传是基于拦截器实现的 --> <package name="File" namespace="/file" extends="struts-default"> <action name="*_*" class="com.maya.file.{1}Action" method="{2}"> <result>{1}_{2}.jsp</result> <result name="input">File_add.jsp</result><!-- 当上传的文件不符合要求是,其struts底层返回的是input --> <!-- 定义一个拦截器,来规定文件上传的规格 --> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param><!-- 定义文件上传的类型 --> <param name="maximumSize">81101</param><!-- 定义文件上传的大小 --> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref><!-- 再一次调用默认的拦截器栈 --> </action> </package>
ActionSupport类
package com.maya.file; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class FileAction extends ActionSupport { private File haha; //文件java.util.File包 private String hahaFileName; //文件名==文件+FileName private String hahaContentType; //获取文件类型,文件+ContentType public File getHaha() { return haha; } public void setHaha(File haha) { this.haha = haha; } public String getHahaFileName() { return hahaFileName; } public void setHahaFileName(String hahaFileName) { this.hahaFileName = hahaFileName; } public String getHahaContentType() { return hahaContentType; } public void setHahaContentType(String hahaContentType) { this.hahaContentType = hahaContentType; } public String insert() throws IOException{ System.out.println("文件名:"+hahaFileName); System.out.println("文件类型:"+hahaContentType); String filePath="C:/Users/Dell-Pc/Desktop/概念车/"+hahaFileName;//设置文件下载位置 File saveFile=new File(filePath);//定义文件 FileUtils.copyFile(haha, saveFile);//将haha复制 return SUCCESS; } }
2.多文件的上传(其实多文件的上传,无非就把单文件上传的那套东西都变成了数组而已)
JSP页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:fielderror></s:fielderror><!-- 接收拦截器返回的错误信息 --> <form action="Files_insert" method="post" enctype="multipart/form-data"> 文件1:<input type="file" name="haha"/><br/> 文件2:<input type="file" name="haha"/><br/> 文件3:<input type="file" name="haha"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
struts.xml文件(同上一样)
ActionSupport类
package com.maya.files; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class FilesAction extends ActionSupport { private File[] haha; //文件java.util.File包 private String[] hahaFileName; //文件名==文件+FileName private String[] hahaContentType; //获取文件类型,文件+ContentType public File[] getHaha() { return haha; } public void setHaha(File[] haha) { this.haha = haha; } public String[] getHahaFileName() { return hahaFileName; } public void setHahaFileName(String[] hahaFileName) { this.hahaFileName = hahaFileName; } public String[] getHahaContentType() { return hahaContentType; } public void setHahaContentType(String[] hahaContentType) { this.hahaContentType = hahaContentType; } public String insert() throws IOException{ for(int i=0;i<haha.length;i++){ System.out.println("文件名:"+hahaFileName[i]); System.out.println("文件类型:"+hahaContentType[i]); String filePath="C:/Users/Dell-Pc/Desktop/概念车/"+hahaFileName[i];//设置文件下载位置 File saveFile=new File(filePath);//定义文件 FileUtils.copyFile(haha[i], saveFile);//将haha复制 } return SUCCESS; } }
3.文件的下载(返回的是文件流)
JSP页面
struts.xml
<!-- 文件下载 --> <action name="download" class="com.maya.files.DownAction"> <result type="stream"><!-- 返回一个流 --> <!-- 文件名--> <param name="contentDisposition">attachment;filename=${fileName}</param> </result> </action>
ActionSupport类
package com.maya.files; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class DownAction extends ActionSupport { private String fileName;//下载文件的名字 public String getFileName() throws Exception{ fileName=new String(fileName.getBytes(),"ISO8859-1");//以为获得的字符集是ISO8859-1,所以要转码 return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getInputStream()throws Exception{//需要实现这个方法 File file=new File("C:/Users/Dell-Pc/Desktop/概念车/哈哈.jpg"); this.fileName="allan"; return new FileInputStream(file);//返回一个文件流 } }