我是靠谱客的博主 怕孤单含羞草,这篇文章主要介绍struts2上传文件不生成临时文件,现在分享给大家,希望可以做个参考。

转载:http://blog.163.com/hehui_326/blog/static/9483177720120392710349/

传文件是很多Web程序都具有的功能。Struts2本身没有提供解析上传文件内容的功能,它使用第三方的文件上传组件提供对文件上传的支持。所以我们要想利用Struts2实现文件上传的功能,首先要将commons-fileupload-1.2.1.jar和commons-io-1.4.jar复制到项目的WEB-INF/lib目录下。

我们知道,Struts1.x的上传组件需要一个ActionForm来辅助传递文件,而Struts2的上传组件却很简单,只用一个拦截器:org.apache.struts2.interceptor.FileUploadInterceptor(这个拦截器不用配置,是自动装载的),它负责调用底层的文件上传组件解析文件内容,并为Action准备与上传文件相关的属性值。这里要强调的是:处理文件上传请求的Action必须提供特殊样式命名的属性。例如,假设表单中文件选择框的名字为upload,那么Action就应该提供以下三个属性upload,uploadFileName,uploadContentType来分别表示上传文件的File对象、上传文件名以及上传文件内容类型。很多人因为忽略了这一点而犯错误。

下面是上传单个文件的JSP页面代码singleUpload.jsp:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>上传单个文件</title> </head> <body> <s:actionerror /> <s:form action="upload" method="post" enctype="multipart/form-data"> <s:file name="upload" label="文件名" /> <s:textfield name="description" label="文件描述" /> <s:submit value="上传" /> </s:form> </body> </html>
注意粗 体部 分的设置,这是有上传 控件 的表单所要求的格式。下面是用于上传的动作类的完整代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.leno.struts2.action; import java.io.*; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; // 代表上传文件的File对象 private File upload; // 上传文件名 private String uploadFileName; // 上传文件的MIME类型 private String uploadContentType; // 上传文件的描述信息 private String description; // 保存上传文件的目录,相对于WEB应用程序的根路径,在struts.xml中配置 private String uploadDir; public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getUploadDir() { return uploadDir; } public void setUploadDir(String uploadDir) { this.uploadDir = uploadDir; } @Override public String execute() throws Exception { String newFileName = null; // 得到当前时间自1970年1月1日0时0分0秒开始走过的毫秒数 long now = System.currentTimeMillis(); // 得到保存上传文件的目录的真实路径 File dir = new File(ServletActionContext.getServletContext() .getRealPath(uploadDir)); // 如果该目录不存在,就创建 if (!dir.exists()) { dir.mkdirs(); } // 为避免重名文件覆盖,判断上传文件是否有扩展名,以时间戳作为新的文件名 int index = uploadFileName.lastIndexOf("."); if (index != -1) { newFileName = now + uploadFileName.substring(index); } else { newFileName = Long.toString(now); } // 读取保存在临时目录下的上传文件,写入到新的文件中 InputStream is = new FileInputStream(upload); OutputStream os = new FileOutputStream(new File(dir, newFileName)); byte[] buf = new byte[1024]; int len = -1; while ((len = is.read(buf)) != -1) { os.write(buf, 0, len); } is.close(); os.close(); return SUCCESS; } }

在execute方法中的实现代码就很简单了,只是从临时文件复制到指定的路径(在这里是web应用程序下的uploadDir目录)中。上传文件的临时目录的默认值是javax.servlet.context.tempdir的值,但可以通过struts.properties(和struts.xml在同一个目录下)的struts.multipart.saveDir属性设置。Struts2上传文件的默认大小限制是2M(2097152字节),也可以通过struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=102400 表示一次上传文件的总大小不能超过100K字节。另一种改变上传属性的方式是在struts.xml中配置constant。本文采用后者。

下面是我们要用到的Struts2的核心配置文件struts.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" " http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 上传文件的临时目录 --> <constant name="struts.multipart.saveDir" value="e:\temp"></constant> <!-- 上传文件的总大小限制 --> <constant name="struts.multipart.maxSize" value="102400"></constant> <!-- 资源文件配置 --> <constant name="struts.custom.i18n.resources" value="ApplicationResources"> </constant> <package name="default" extends="struts-default"> <action name="upload" class="org.leno.struts2.action.UploadAction"> <!—文件上传拦截器 --> <interceptor-ref name="defaultStack"> <!-- 设置Action能接受的文件的最大长度,而不是对上传文件的最大长度进行限制。 (因为在Action处理之前,文件已经上传到服务器了。) --> <param name="fileUpload.maximumSize">102400</param> <param name="fileUpload.allowedTypes"> image/gif,image/jpeg,image/pjpeg </param> </interceptor-ref> <result name="success">/success.jsp</result> <result name="input">/singleUpload.jsp</result> <param name="uploadDir">/WEB-INF/UploadFiles</param> </action> </package> </struts>

当我们对文件上传进行了更多的控制,上传的文件不满足所指定的限制条件时,我们可以使用特定的I18N键添加相关的错误消息。在src下新建ApplicationResources.properties:

struts.messages.error.uploading=文件上传错误

struts.messages.error.file.too.large=文件上传长度超过了限制的长度

struts.messages.error.content.type.not.allowed=不容许上传这种类型的文件

这样,上传文件如果出错,框架去会自动导向到input结果页面,同时显示错误信息;如果成功,就可以导航到success.jsp。我们可以在success.jsp页中通过<s:property>获得文件的属性(文件名,文件内容类型,文件描述以及文件的长度),代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>上传成功</title> </head> <body> <h1>上传成功,文件信息如下:</h1> 文件名:<s:property value="uploadFileName" /><br/> 文件大小:<s:property value="upload.length()" /><br/> 文件类型:<s:property value="uploadContentType" /><br/> 文件描述:<s:property value="description" /><br/> </body> </html>



最后

以上就是怕孤单含羞草最近收集整理的关于struts2上传文件不生成临时文件的全部内容,更多相关struts2上传文件不生成临时文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部