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>