`

Struts2文件上传

阅读更多

Struts2使用开源项目Apache Jakarta Commons FileUpload和内建的FileUploadInterceptor拦截器实现文件上传,所需的jar包如下:

commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.3.1.jar
commons-fileupload-1.2.jar

★ 文件上传页面 fileupload.jsp

  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4. <html>  
  5.   <head>  
  6.   <head>  
  7.      
  8.   <body>  
  9.     <s:form action="fileUpload" method="post" enctype="multipart/form-data">  
  10.       <s:file name="doc" label="File"/>  
  11.       <s:submit/>  
  12.     <s:form>  
  13.   <body>  
  14. <html>  

这里需要注意的是,form的enctype属性必须设置为multipart/form-data。

★ 处理文件上传 FileUploadAction.java

  1. package fileupload;   
  2.   
  3. import java.io.File;   
  4. import java.text.DateFormat;   
  5. import java.text.SimpleDateFormat;   
  6. import java.util.Date;   
  7. import java.util.Random;   
  8.   
  9. import javax.servlet.ServletContext;   
  10.   
  11. import org.apache.commons.io.FileUtils;   
  12. import org.apache.struts2.util.ServletContextAware;   
  13.   
  14. import com.opensymphony.xwork2.ActionSupport;   
  15.   
  16. public class FileUploadAction extends ActionSupport implements ServletContextAware {   
  17.        
  18.     private static final long serialVersionUID = -5016873153441103539L;   
  19.        
  20.     private File doc;   
  21.     private String fileName;   
  22.     private String contentType;   
  23.        
  24.     private ServletContext context;   
  25.            
  26.     public void setDoc(File file) {   
  27.         this.doc = file;   
  28.     }   
  29.        
  30.     public void setDocFileName(String fileName) {   
  31.         this.fileName = fileName;   
  32.     }   
  33.        
  34.     public void setDocContentType(String contentType) {   
  35.         this.contentType = contentType;   
  36.     }   
  37.        
  38.     public void setServletContext(ServletContext context) {   
  39.         this.context = context;   
  40.     }   
  41.        
  42.     public String execute() throws Exception {   
  43.         String targetDirectory = context.getRealPath("/upload");   
  44.         String targetFileName = generateFileName(fileName);   
  45.         File target = new File(targetDirectory, targetFileName);   
  46.            
  47.         FileUtils.copyFile(doc, target);   
  48.            
  49.         return SUCCESS;   
  50.     }   
  51.        
  52.     private String generateFileName(String fileName) {   
  53.         DateFormat format = new SimpleDateFormat("yyMMddHHmmss");   
  54.         String formatDate = format.format(new Date());   
  55.            
  56.         int random = new Random().nextInt(10000);   
  57.            
  58.         int position = fileName.lastIndexOf(".");   
  59.         String extension = fileName.substring(position);   
  60.            
  61.         return formatDate + random + extension;   
  62.     }      
  63. }  

在fileupload.jsp中,只有doc一个字段,而FileUploadAction.java中,却有三个字段,Struts2怎么通过页面的一个字段设置Action里的三个字段呢?没错,这就是FileUploadInterceptor的功劳了!你所要做的只是按照一定的样式命名这三个字段的set方法,而字段名可以任意命名。第一个File类型的字段的set方法还是以常规的方式命名,另两个String类型的字段的set方法必须分别以“File字段的set方法名+FileName”和“File字段的set方法名+ContentType”来命名。

★ 配置文件 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.   
  6. <struts>  
  7.   
  8.     <package name="fileupload" namespace="/fileupload" extends="struts-default">  
  9.            
  10.         <action name="fileUpload" class="fileupload.FileUploadAction">  
  11.             <result>/fileupload/upload_success.jsp</result>  
  12.         </action>  
  13.        
  14.     </package>  
  15.   
  16. </struts>  

★ 配置文件 web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.        
  8.     <filter>  
  9.         <filter-name>struts-cleanup</filter-name>  
  10.         <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>  
  11.     </filter>  
  12.        
  13.     <filter>  
  14.         <filter-name>struts2</filter-name>  
  15.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  16.     </filter>  
  17.        
  18.     <filter-mapping>  
  19.         <filter-name>struts-cleanup</filter-name>  
  20.         <url-pattern>/*</url-pattern>  
  21.     </filter-mapping>  
  22.        
  23.     <filter-mapping>  
  24.         <filter-name>struts2</filter-name>  
  25.         <url-pattern>/*</url-pattern>  
  26.     </filter-mapping>  
  27.   
  28. </web-app>  

分享到:
评论

相关推荐

    struts2文件上传和下载

    struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载

    struts2文件上传

    struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传

    struts2 文件上传

    struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload

    struts2文件上传实例

    struts2文件上传实例,程序员宝典......

    struts2文件上传jar

    里面包括的是实现struts2文件上传所需要的全部jar包

    Struts2 文件上传

    Struts2 文件上传

    Struts2文件上传源码

    Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码

    struts2文件上传与下载

    struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!

    struts2 文件上传功能

    Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...

    struts2文件上传下载源代码

    http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接

Global site tag (gtag.js) - Google Analytics