Struts2 单个文件上传
不考虑界面的显示,只是做一个功能的实现
需要注意的是Jsp中上传文件的名字(A)和处理文件上传类中的命名(A)需要一致。相应的另外两个属性:AContentType 和 AFileName 也是必须的。
这里只是将客户端文件流与服务端文件流建立连接,保存该文件流,就需要自己去实现,所以,需要做持久化的操作。
jsp页面端:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <form action="fileAction" method="post" enctype="multipart/form-data"> 2 <table border="1" cellpadding="1" cellspacing="1" align="center"> 3 <tr> 4 <th>文件上传</th> 5 </tr> 6 <tr> 7 <td><input type="file" name="fileModel.fileUpload"/></td> 8 </tr> 9 <tr> 10 <td><input type="submit" value="submit" /></td> 11 </tr> 12 </table> 13 </form>
对应的Action处理:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.test.action; 2 3 import cn.test.vo.FileModel; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 @SuppressWarnings("serial") 8 public class FileHandle extends ActionSupport { 9 private FileModel fileModel; 10 11 public String fileSave(){ 12 if (fileModel.saveFile()) { 13 return SUCCESS; 14 } 15 return INPUT ; 16 } 17 18 public FileModel getFileModel() { 19 return fileModel; 20 } 21 22 public void setFileModel(FileModel fileModel) { 23 this.fileModel = fileModel; 24 } 25 26 }
针对文件上传的处理类(实现复用):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.test.vo; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.Date; 6 7 import org.apache.commons.io.FileUtils; 8 import org.apache.struts2.ServletActionContext; 9 10 import com.opensymphony.xwork2.ActionContext; 11 12 public class FileModel { 13 private File fileUpload ; //上传的文件流 14 private String fileUploadContentType ; //上传文件的类型 15 private String fileUploadFileName ; //上传文件的名字 16 private String savedFilePath ; //将文件上传到本地的文件夹下 17 private String relativeFilePath ; //上传的文件的相对路径 18 /** 19 * 将上传的文件持久化到本地指定的文件夹上 20 * @return 21 */ 22 public boolean saveFile(){ 23 boolean flag = false ; 24 String newFileName = new Date().getTime()+getExtention(fileUploadFileName); 25 String realPath = ServletActionContext 26 .getServletContext() 27 .getRealPath(savedFilePath)+"/"+newFileName; 28 relativeFilePath = realPath.substring(ServletActionContext 29 .getServletContext() 30 .getRealPath("/") 31 .length()); 32 if (fileUpload != null) { 33 File savedFile = new File(realPath); 34 if (!savedFile.getParentFile().exists()) { 35 savedFile.getParentFile().mkdirs(); 36 } 37 try { 38 FileUtils.copyFile(fileUpload, savedFile); 39 ActionContext.getContext().put("message", "文件上传成功"); 40 flag = true ; 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 } 45 return flag ; 46 } 47 /** 48 * 将提交的文件,以当前时间重新命名,防止文件的命名冲突 49 * @param fileName 上传的文件名 50 * @return 51 */ 52 public String getExtention(String fileName){ 53 int pos = fileName.lastIndexOf("."); 54 return fileName.substring(pos); 55 } 56 57 public File getFileUpload() { 58 return fileUpload; 59 } 60 public void setFileUpload(File fileUpload) { 61 this.fileUpload = fileUpload; 62 } 63 public String getFileUploadContentType() { 64 return fileUploadContentType; 65 } 66 public void setFileUploadContentType(String fileUploadContentType) { 67 this.fileUploadContentType = fileUploadContentType; 68 } 69 public String getFileUploadFileName() { 70 return fileUploadFileName; 71 } 72 public void setFileUploadFileName(String fileUploadFileName) { 73 this.fileUploadFileName = fileUploadFileName; 74 } 75 public String getSavedFilePath() { 76 return savedFilePath; 77 } 78 public void setSavedFilePath(String savedFilePath) { 79 this.savedFilePath = savedFilePath; 80 } 81 82 }
web.xml 的配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>news_s2sh</display-name> 4 5 <filter> 6 <filter-name>struts2</filter-name> 7 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 8 </filter> 9 <filter-mapping> 10 <filter-name>struts2</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping> 13 14 <welcome-file-list> 15 <welcome-file>index.html</welcome-file> 16 <welcome-file>index.htm</welcome-file> 17 <welcome-file>index.jsp</welcome-file> 18 </welcome-file-list> 19 </web-app>
Struts.xml 的配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 6 <struts> 7 8 <constant name="struts.devMode" value="true" /> 9 <constant name="struts.i18n.encoding" value="utf-8" /> 10 11 <package name="default" namespace="/" extends="struts-default"> 12 <action name="fileAction" class="cn.test.action.FileHandle" method="fileSave"> 13 <!-- fileModel.savedFilePath 表示文件保存的文件夹的相对路径 --> 14 <param name="fileModel.savedFilePath">/file</param> 15 <result name="input">/index.jsp</result> 16 <result>/success.jsp</result> 17 </action> 18 </package> 19 20 </struts>