Struts文件上传机制
单文件上传
1.Action类
1 package FileUpload; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 8 import javax.servlet.ServletContext; 9 10 import org.apache.struts2.ServletActionContext; 11 12 import com.opensymphony.xwork2.ActionSupport; 13 14 public class FileUpload extends ActionSupport{ 15 16 private File fileName; 17 private String fileNameContentType; 18 private String fileNameFileName; 19 20 public String execute() throws Exception{ 21 22 ServletContext servletContext = ServletActionContext.getServletContext(); 23 String dir = servletContext.getRealPath("/files/"+fileNameFileName); 24 System.out.println(dir);
25 FileOutputStream outputStream = new FileOutputStream(dir); 26 FileInputStream inputStream = new FileInputStream(fileName); 27 byte[] buffer = new byte[1024]; 28 int len = 0; 29 // 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中.读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。 30 while((len = inputStream.read(buffer))!=-1){ 31 // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 32 outputStream.write(buffer, 0, len); 33 } 34 inputStream.close(); 35 outputStream.close();
36 return SUCCESS; 37 } 38 39 public File getFileName() { 40 return fileName; 41 } 42 43 public void setFileName(File fileName) { 44 this.fileName = fileName; 45 } 46 47 public String getFileNameContentType() { 48 return fileNameContentType; 49 } 50 51 public void setFileNameContentType(String fileNameContentType) { 52 this.fileNameContentType = fileNameContentType; 53 } 54 55 public String getFileNameFileName() { 56 return fileNameFileName; 57 } 58 59 public void setFileNameFileName(String fileNameFileName) { 60 this.fileNameFileName = fileNameFileName; 61 } 62 63 64 65 }
2.
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 9 <form action="/Struts2FileUpload/fileUp.action" method="post" enctype ="multipart/form-data"> 10 11 <input type="file" name="fileName" > 12 <input type="submit" value="提交"> 13 </form> 14 15 </body> 16 </html>
3.
<action name="fileUp" class="FileUpload.FileUpload"> <result>/message.jsp</result> </action>
多文件上传
可以用数组,也可以用list集合
1.HTML
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 9 <form action="/Struts2FileUpload/filesUp.action" method="post" enctype ="multipart/form-data"> 10 11 <input type="file" name="fileName" > 12 <input type="file" name="fileName" > 13 <input type="file" name="fileName" > 14 <input type="submit" value="提交"> 15 </form> 16 17 </body> 18 </html>
注意:input的name名字一样
2.Action接收请求
可以用数组接收,也可以用集合接收
1 package FileUpload; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 8 import javax.servlet.ServletContext; 9 10 import org.apache.struts2.ServletActionContext; 11 12 import com.opensymphony.xwork2.ActionSupport; 13 14 public class FilesUpload extends ActionSupport{ 15 16 private File[] fileName; 17 private String[] fileNameContentType; 18 private String[] fileNameFileName; 19 20 public String execute() throws Exception{ 21 22 for(int i=0;i<fileName.length;i++){ 23 ServletContext servletContext = ServletActionContext.getServletContext(); 24 String dir = servletContext.getRealPath("/files/"+fileNameFileName[i]); 25 System.out.println(dir); 26 FileOutputStream outputStream = new FileOutputStream(dir); 27 FileInputStream inputStream = new FileInputStream(fileName[i]); 28 byte[] buffer = new byte[1024]; 29 int len = 0; 30 // 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中.读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。 31 while((len = inputStream.read(buffer))!=-1){ 32 // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 33 outputStream.write(buffer, 0, len); 34 } 35 inputStream.close(); 36 outputStream.close(); 37 } 38 return SUCCESS; 39 } 40 41 public File[] getFileName() { 42 return fileName; 43 } 44 45 public void setFileName(File[] fileName) { 46 this.fileName = fileName; 47 } 48 49 public String[] getFileNameContentType() { 50 return fileNameContentType; 51 } 52 53 public void setFileNameContentType(String[] fileNameContentType) { 54 this.fileNameContentType = fileNameContentType; 55 } 56 57 public String[] getFileNameFileName() { 58 return fileNameFileName; 59 } 60 61 public void setFileNameFileName(String[] fileNameFileName) { 62 this.fileNameFileName = fileNameFileName; 63 } 64 65 66 67 68 }
3.测试结果
3.通过配置设置文件上传类型,大小,后缀名
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.devMode" value="true"></constant> 9 10 11 <!-- package命名空间 --> 12 <package name="test" namespace="/" extends="struts-default"> 13 14 <!-- 配置允许文件上传类型与大小的配置 --> 15 <interceptors> 16 17 <!-- 配置拦截器 --> 18 <interceptor-stack name="atguigustack"> 19 <interceptor-ref name="defaultStack"> 20 <!-- 设置单个文件上传大小 --> 21 <param name="fileUpload.maximumSize">5000000</param> 22 <!-- 设置文件类型,可以先上传一个,打印出contentType --> 23 <param name="fileUpload.allowedTypes">text/html,text/xml,application/pdf</param> 24 <!-- 设置允许上传的文件后缀名 --> 25 <param name="fileUpload.allowedExtensions">html,dtd,xml,zip,pdf</param> 26 </interceptor-ref> 27 </interceptor-stack> 28 </interceptors> 29 30 <!-- 应用拦截器 --> 31 <default-interceptor-ref name="atguigustack"></default-interceptor-ref> 32 33 <action name="fileUpload" class="fileUpload"> 34 <result>/index.jsp</result> 35 </action> 36 37 <action name="fileUpload2" class="fileUpload2"> 38 <result>/index.jsp</result> 39 </action> 40 41 42 <action name="fileUp" class="FileUpload.FileUpload"> 43 <result>/message.jsp</result> 44 </action> 45 46 <action name="filesUp" class="FileUpload.FilesUpload"> 47 <result>/message.jsp</result> 48 </action> 49 50 51 52 </package> 53 54 </struts>
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】