Struts2自己並沒提供上傳的組件,咱們能夠經過調用上傳框架來實現文件的上傳。java
1、配置上傳解析器apache
首先要配置項目的框架,也就是倒導入"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常量值。app
修改常量值有兩種方法,一是在"struts.xml"中修改,代碼以下:框架
<constant name="struts.multipart.paeser" value="cos"></constant>jsp
struts.properties中修改,代碼以下:ide
sruts.multipart.parser=cospost
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)。
this
二.功能實現
upload.jsp
- <form action="upload.action" method="post" enctype="multipart/form-data">
- <input type="file" name="upFile" />
- <input type="submit" value="上傳" />
- </form>
UploadAction.java
- package com.travelsky.action;
- import java.io.*;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class UploadAction extends ActionSupport {
- private File upFile; //獲取文件
- private String upFileFileName;// 獲取上傳文件名稱
- private String upFileContentType;// 獲取上傳文件類型
- public File getUpFile() {
- return upFile;
- }
- public void setUpFile(File upFile) {
- this.upFile = upFile;
- }
- public String getUpFileFileName() {
- return upFileFileName;
- }
- public void setUpFileFileName(String upFileFileName) {
- this.upFileFileName = upFileFileName;
- }
- public String getUpFileContentType() {
- return upFileContentType;
- }
- public void setUpFileContentType(String upFileContentType) {
- this.upFileContentType = upFileContentType;
- }
- @Override
- public String execute() throws Exception {
- String path = ServletActionContext.getServletContext().getRealPath(
- "/p_w_picpaths");
- System.out.println(path);
- if (upFile != null) {
- File savefile = new File(new File(path), upFileFileName);
- if (!savefile.getParentFile().exists())
- savefile.getParentFile().mkdirs();
- try {
- InputStream is = new FileInputStream(upFile);
- // 建立一個輸出流
- OutputStream os = new FileOutputStream(savefile);
- // 設置緩存
- byte[] buffer = new byte[1024];
- int length = 0;
- // 讀取myFile文件輸出到toFile文件中
- while ((length = is.read(buffer)) > 0) {
- os.write(buffer, 0, length);
- }
- // 關閉輸入流
- is.close();
- // 關閉輸出流
- os.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return SUCCESS;
- }
- }
structs.xml
- <struts>
- <constant name="struts.multipart.paeser" value="cos"></constant>
- <action name="upload" class="com.travelsky.action.UploadAction">
- <result name="success">upload.jsp</result>
- </action>
- </package>
- </struts>