这俩天由于项目需要,学习了上传文件框架,有dwr的异步上传文件,对于异步传输确实方便,但不适合form中含有文件上传的需求。也学了apache最原本的文件上传,可以满足form中含有文件上传的需求,还有struts2,个人感觉最方便了,代码简单,而且可以在struts.xml直接对上传文件的格式,大小直接进行配置,官方文档写得很好,现在根据官方文档学习的东西总结如下:
需要的依赖包:
Library | URL | Struts 2.0.x | Struts 2.1.x |
---|---|---|---|
Commons-FileUpload | http://commons.apache.org/fileupload/ | 1.1.1 | 1.2.1 |
Commons-IO | http://commons.apache.org/io/ | 1.0 | 1.3.2 |
<
action
name
=
"doUpload"
class
=
"com.example.UploadAction"
>
<
result
name
=
"success"
>good_result.jsp</
result
>
</
action
>
|
enctype
=
"multipart/form-data"将文件编码成二进制流,以免后台接收到的file=null
默认是
enctype="application/x-www-form-urlencoded",以url传输
<
s:form
action
=
"doUpload"
method
=
"post"
enctype
=
"multipart/form-data"
>
<
s:file
name
=
"upload"
label
=
"File"
/>
<
s:submit
/>
</
s:form
>
package
com.example;
import
java.io.File;
import
com.opensymphony.xwork2.ActionSupport;
public
class
UploadAction
extends
ActionSupport {
private
File file;
private
String contentType;
private
String filename;
public
void
setUpload(File file) {
this
.file = file;
}
public
void
setUploadContentType(String contentType) {
this
.contentType = contentType;
}
public
void
setUploadFileName(String filename) {
this
.filename = filename;
}
public
String execute() {
//...
return
SUCCESS;
}
}
|
这时候,你就可以在execute()进行文件的上传操作了,代码如下
System.out.println("fileName=" + filename + " fileType=" + contentType
+ " desc=" + request.getParameter("desc"));
// 获得本地服务器存放位置,若路径没有则直接创建一个ProjectDemoImg
String serverFileDir = request.getServletContext().getRealPath(
"/ProjectDemoImg");
// 获得服务的访问路径
String servURL = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + request.getContextPath();
try {
// 用封装好的file将保存在缓存中的file直接copy到目的地
File destFile = new File(serverFileDir + "/" + filename);
FileUtils.copyFile(file, destFile);
} catch (IOException e) {
e.printStackTrace();
}
这里要注意的是,文件上传的时候,提交到actioin那边,strtut2是把输入流直接封装成一个file,这个file默认是在
javax.servlet.context.tempdir,你可以在设置,下面讨论
下载说下struts.xml配置来控制文件上传的格式和大小以及文件上传时暂放的目录设置
在default.properties文件中可以看到相关的默认设置,
struts.multipart.parser=jakarta
struts.multipart.saveDir=
struts.multipart.maxSize=2097152
|
<
constant
name
=
"struts.multipart.maxSize"
value
=
"1000000"
/>这里是以字节为单位
<
constant
name
=
"struts.multipart.saveDi"
value
=
"D:\"
/>暂放的文件是在d盘
在action的拦截器里,我们也可以设置
<
action
name
=
"doUpload"
class
=
"com.example.UploadAction"
>
<
interceptor-ref
name
=
"basicStack"
/>
<
interceptor-ref
name
=
"fileUpload"
>
<
param
name
=
"allowedTypes"
>text/plain</
param
>
</
interceptor-ref
>
<
interceptor-ref
name
=
"validation"
/>
<
interceptor-ref
name
=
"workflow"
/>
<
result
name
=
"success"
>good_result.jsp</
result
>
<interceptor-ref name="defaultStack"/>
</
action
>
|
至于allowedTypes我们可以在tomcat的web.xml中查看到,文件拓展名对应的格式,下面列举一些
<mime-mapping>
<extension>json</extension>
<mime-type>application/json</mime-type>
</mime-mapping>
<mime-mapping>
<extension>java</extension>
<mime-type>text/x-java-source</mime-type>
</mime-mapping>
<mime-mapping>
<extension>jpg</extension>
<mime-type>image/jpeg</mime-type>
</mime-mapping>
<mime-mapping>
<extension>txt</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>
说到这里,单文件上传的问题就基本解决了,在这里,你可能会遇到"
No result defined for action com.gen.action.ProjectDemoAction and result input "
这是因为在strut2.xml配置的时候,你没有配置他出现form文件类型出错时,处理之后返回的页面
默认你要在action中加上<result name="input">/...jsp</result>.问题解决,若要在前台提示错误信息,
加上<s:fieldError/>.这就是struts2的单文件上传,至于多文件上传看官方文档,写得很好