您的位置:首页 > 编程语言 > Java开发

struts2-上传文件(1)

2008-08-07 22:51 405 查看
上传文件是很多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必须提供特殊样式命名的属性[/b]。例如,假设表单中文件选择框的名字为upload,那么Action就应该提供以下三个属性upload,uploadFileName,uploadContentType来分别表示上传文件的File对象、上传文件名以及上传文件内容类型。很多人因为忽略了这一点而犯错误。
下面是上传单个文件的JSP页面代码singleUpload.jsp:
<%@ 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[/b]=[/b]"post" [/b]enctype[/b]=[/b]"multipart/form-data"[/b]>
<s:file name="upload" label="文件名" />
<s:textfield name="description" label="文件描述" />
<s:submit value="上传" />
</s:form>
</body>
</html>

注意粗体部分的设置,这是有上传控件的表单所要求的格式。下面是用于上传的动作类的完整代码:
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
<?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>获得文件的属性(文件名,文件内容类型,文件描述以及文件的长度),代码如下:
<%@ 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: