Struts2本身并没提供上传的组件,我们可以通过调用上传框架来实现文件的上传。

一、配置上传解析器

首先要配置项目的框架,也就是倒导入"struts2-core-2.2.1.jar"库文件,找到org.apache.struts2包下的default.porperties资源文件。如下图;资源文件中给出了不同的strus2的默认配置,我们可看到struts2默认是jakarta作为其文件上传的解析器。


jakarta是Commo-FileUpload的框架。如果要使用Commo-FileUpload框架来上传文件,只需将"commons-fileupload-1.2.1.jar"和"commons-io-1.3.2.jar"两个jar复制到项目中的WEB-INF/lib目录下就可。

如果想要使用COS框架来上传文件,只需将“cos.jar”复制到项目中就可以,然后在修改struts.multipart.parser常量值。

修改常量值有两种方法,一是在"struts.xml"中修改,代码如下:

<constant name="struts.multipart.paeser" value="cos"></constant>

struts.properties中修改,代码如下:

sruts.multipart.parser=cos

form的enctype属性为编码方式,常用有两种:application/x-www-form-
urlencoded和multipart/form-data,默认为application/x-www-form-
urlencoded。
当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据
转换成一个字串(name1=value1&name2=value2...),然后把这个字串append到
url后面,用?分割,加载这个新的url。
当action为post时候,浏览器把form数据封装到http body中,然后发送到server。
如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。
但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以
控件为单位分割,并为每个部分加上Content-Disposition(form-data或者
file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符
(boundary)。
 

二.功能实现

upload.jsp

 
  
  1. <form action="upload.action" method="post" enctype="multipart/form-data"> 
  2.             <input type="file" name="upFile" /> 
  3.             <input type="submit" value="上传" /> 
  4.         </form> 

UploadAction.java

 

 
  
  1. package com.travelsky.action;  
  2.  
  3. import java.io.*;  
  4.  
  5. import org.apache.struts2.ServletActionContext;  
  6.  
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.  
  9. public class UploadAction extends ActionSupport {  
  10.     private File upFile;          //获取文件  
  11.     private String upFileFileName;// 获取上传文件名称  
  12.     private String upFileContentType;// 获取上传文件类型  
  13.  
  14.     public File getUpFile() {  
  15.         return upFile;  
  16.     }  
  17.  
  18.     public void setUpFile(File upFile) {  
  19.         this.upFile = upFile;  
  20.     }  
  21.  
  22.     public String getUpFileFileName() {  
  23.         return upFileFileName;  
  24.     }  
  25.  
  26.     public void setUpFileFileName(String upFileFileName) {  
  27.         this.upFileFileName = upFileFileName;  
  28.     }  
  29.  
  30.     public String getUpFileContentType() {  
  31.         return upFileContentType;  
  32.     }  
  33.  
  34.     public void setUpFileContentType(String upFileContentType) {  
  35.         this.upFileContentType = upFileContentType;  
  36.     }  
  37.  
  38.     @Override 
  39.     public String execute() throws Exception {  
  40.         String path = ServletActionContext.getServletContext().getRealPath(  
  41.                 "/p_w_picpaths");  
  42.  
  43.         System.out.println(path);  
  44.         if (upFile != null) {  
  45.             File savefile = new File(new File(path), upFileFileName);  
  46.             if (!savefile.getParentFile().exists())  
  47.                 savefile.getParentFile().mkdirs();  
  48.             try {  
  49.                 InputStream is = new FileInputStream(upFile);    
  50.                 // 创建一个输出流  
  51.                 OutputStream os = new FileOutputStream(savefile);  
  52.  
  53.                 // 设置缓存  
  54.                 byte[] buffer = new byte[1024];  
  55.  
  56.                 int length = 0;  
  57.  
  58.                 // 读取myFile文件输出到toFile文件中  
  59.                 while ((length = is.read(buffer)) > 0) {  
  60.                     os.write(buffer, 0, length);  
  61.                 }  
  62.                 // 关闭输入流  
  63.                 is.close();  
  64.                 // 关闭输出流  
  65.                 os.close();  
  66.  
  67.             } catch (IOException e) {  
  68.                 // TODO Auto-generated catch block  
  69.                 e.printStackTrace();  
  70.             }  
  71.         }  
  72.  
  73.         return SUCCESS;  
  74.     }  
  75.  
  76. }  

structs.xml

 

 
  
  1. <struts> 
  2.     <constant name="struts.multipart.paeser" value="cos"></constant> 
  3. <action name="upload" class="com.travelsky.action.UploadAction"> 
  4.         <result name="success">upload.jsp</result> 
  5.         </action> 
  6.     </package> 
  7. </struts>