关于Struts2文件上传的问题,首先要准备好一下几个包

Struts2 上传文件_struts2

新建一个项目,整体架构如下:

Struts2 上传文件_java_02

接着就通过一下实例来实现吧。

 1、UploadAction.java

  1. package com.action; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.FileOutputStream; 
  6.  
  7. import org.apache.struts2.ServletActionContext; 
  8.  
  9. import com.opensymphony.xwork2.ActionSupport; 
  10.  
  11. public class UploadAction extends ActionSupport{ 
  12.     private String title;   //封装文件标题请求参数的属性 
  13.      
  14.     private File upload;    //封装上传文件域的属性 
  15.      
  16.     private String uploadContentType;   //封装上传文件类型的属性 
  17.      
  18.     private String uploadFileName;      //封装上传文件名的属性 
  19.      
  20.     private String savePath;    //直接在Struts.xml文件中配置的属性      
  21.      
  22.     @Override 
  23.     public String execute() throws Exception{ 
  24.         FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName()); 
  25.          
  26.         /*FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext() 
  27.                                                 .getRealPath(getSavePath())+ "\\" + getUploadFileName());*/ 
  28.          
  29.         //FileOutputStream fos = new FileOutputStream("e:\\upload" + "\\" + getUploadFileName()); 
  30.         FileInputStream fis = new FileInputStream(getUpload()); 
  31.         byte[] buffer = new byte[1024]; 
  32.         int len = 0
  33.         while((len = fis.read(buffer)) > 0){ 
  34.             fos.write(buffer, 0, len); 
  35.         } 
  36.         return SUCCESS; 
  37.     } 
  38.      
  39.     public String getTitle() { 
  40.         return title; 
  41.     } 
  42.  
  43.     public void setTitle(String title) { 
  44.         this.title = title; 
  45.     } 
  46.  
  47.     public File getUpload() { 
  48.         return upload; 
  49.     } 
  50.  
  51.     public void setUpload(File upload) { 
  52.         this.upload = upload; 
  53.     } 
  54.  
  55.     public String getUploadContentType() { 
  56.         return uploadContentType; 
  57.     } 
  58.  
  59.     public void setUploadContentType(String uploadContentType) { 
  60.         this.uploadContentType = uploadContentType; 
  61.     } 
  62.  
  63.     public String getUploadFileName() { 
  64.         return uploadFileName; 
  65.     } 
  66.  
  67.     public void setUploadFileName(String uploadFileName) { 
  68.         this.uploadFileName = uploadFileName; 
  69.     } 
  70.  
  71.     /** 
  72.      * 返回上传文件的保存 
  73.      * @return 
  74.      */ 
  75.     public String getSavePath() { 
  76.         return ServletActionContext.getServletContext().getRealPath(savePath); 
  77.     } 
  78.  
  79.     public void setSavePath(String savePath) { 
  80.         this.savePath = savePath; 
  81.     } 

 

2、Struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 
  3. <struts> 
  4.     <constant name="struts.custom.i18n.resources" value="mess" /> 
  5.     <constant name="struts.i18n.encoding" value="UTF-8" /> 
  6.     <constant name="struts.devMode" value="true"></constant> 
  7.     <package name="lee" extends="json-default"> 
  8.         <action name="uploadAction" class="com.action.UploadAction"> 
  9.             <param name="savePath">/upload</param> 
  10.             <result>success.jsp</result> 
  11.         </action> 
  12.     </package> 
  13. </struts>     

3、index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <
  3. String path = request.getContextPath(); 
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  5. %> 
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  8. <html> 
  9.   <head> 
  10.     <base href="<%=basePath%>"> 
  11.      
  12.     <title>文件上传</title> 
  13.      
  14.   </head> 
  15.    
  16.   <body> 
  17.     <form action="uploadAction.action" method="post" enctype="multipart/form-data"> 
  18.         文件名称 : <input type="text" name="title"><br> 
  19.         文          件 :<input type="file" name="upload"><br> 
  20.         <input type="submit" value=" 上传 "> 
  21.     </form> 
  22.   </body> 
  23. </html> 

4、Success.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <
  3. String path = request.getContextPath(); 
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  5. %> 
  6. <%@taglib prefix="s" uri="/struts-tags" %> 
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  8. <html> 
  9.   <head> 
  10.     <base href="<%=basePath%>">     
  11.     <title>上传成功</title> 
  12.   </head> 
  13.    
  14.   <body> 
  15.     文件<b>${uploadFileName}</b>上传成功! <br> 
  16.   </body> 
  17. </html> 

然后部署后运行效果图如下:

Struts2 上传文件_文件上传_03

 

点击上传文件后:

Struts2 上传文件_struts2_04