您的位置:首页 > 其它

Structs2实现文件上传功能

2012-08-25 16:44 211 查看
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

<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(
"/images");

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  upload structs