- 浏览: 243779 次
- 性别:
- 来自: 北京
最新评论
-
java_frog:
我之前resteasy用的3.6.0,周末过来降成3.1.0, ...
在本地环境ok,预发环境就老报这个,版本应该也没问题,有碰到过的人吗 -
yugouai:
另外还有可能是拿不到环境变量的,所以crontab的shell ...
linux下crontab定时任务不执行 -
wuchsh2013:
最好不要写在脚本里面,在执行脚本前设置权限。
linux下crontab定时任务不执行 -
原水小子:
给力哈~~~~~~~~~~~~~~
正则表达式限制输入框只能输入数字 -
qizhijun:
请问你找到原因了吗?我也遇到同样的问题。头疼好几天了。
java调用FlashPaper时printing failed for an unknown reason错误
上传文件是很多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:
<%@ 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>
注意粗体部分的设置,这是有上传控件的表单所要求的格式。下面是用于上传的动作类的完整代码:
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>
我们知道,Struts1.x的上传组件需要一个ActionForm来辅助传递文件,而Struts2的上传组件却很简单,只用一个拦截器:org.apache.struts2.interceptor.FileUploadInterceptor(这个拦截器不用配置,是自动装载的),它负责调用底层的文件上传组件解析文件内容,并为Action准备与上传文件相关的属性值。这里要强调的是:处理文件上传请求的Action必须提供特殊样式命名的属性。例如,假设表单中文件选择框的名字为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="post" enctype="multipart/form-data">
<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>
发表评论
-
生成AES密钥,网摘自留
2015-01-26 15:46 1068import java.security.NoSu ... -
转 BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
2014-11-14 20:37 662BeanUtils.copyProperties与Proper ... -
tar命令--摘
2014-04-18 11:21 514tar 解压缩命令 tar -c: ... -
spring为ApplicationContext提供有三种实现(举例) (转)
2013-08-01 08:42 690spring为ApplicationContext提供的3种实 ... -
转:F5 APACHE WEBLOGIC取用户真实IP的问题
2012-10-23 08:32 1557应用环境如下: F5负载均衡16台apache,然后转到后台 ... -
查看端口被哪程序占用
2011-04-22 16:39 881怎么查看端口占用情况? 开始--运行--cmd ... -
转:Spring中任务调度cronExpression配置说明
2010-06-25 12:56 673cronExpression配置说明 字段 允许值 ... -
flv 播放器
2009-10-11 15:36 1402flv 播放器 <script type="t ... -
domino介绍
2009-08-10 11:53 2140.前言 DOMINO是一个以电子邮件为基础发展起来的标准群 ... -
javadoc,在 Java 的注释上做文章-(转)
2009-07-21 18:58 738前言 一. Java 文档 ... -
转---MySQL导出导入数据库命令
2009-07-21 13:48 11521.mysql导出整个数据库 mysqldump ... -
myeclipse7.5注册码
2009-07-20 16:43 5341前几天装了一个myeclipse7.5,每天弹要注册,于是在网 ... -
IBM WebShere Portal主题与皮肤开发
2009-07-06 15:50 3348所谓主题与皮肤,主要指门户系统的界面风格与显示方式。在企业门户 ... -
IE与FireFox的js和css
2009-07-01 10:56 918png透明 AlphaImageLoader filter:p ... -
wpf和wdf的区别
2009-06-25 17:46 1983wpf&&wdf是两 ... -
top、clientTop、scrollTop、offsetTop
2009-06-17 20:49 839<html> <head> < ... -
marquee
2009-06-15 19:09 1056功能: 使文字或者图片 ... -
marquee参数详解
2009-06-15 19:08 19492008-04-25 09:51(文字滚动)参数详解 文字 ... -
onmousemove、onmouseover、 onmouseup及onclick的区别
2009-06-15 15:28 2001时间上 onmousemove 事件触发后,再触发 onmou ... -
eclipse快捷键
2009-06-03 16:18 745Eclipse 常用快捷键 Eclipse ...
相关推荐
第八章:struts2文件上传下载.ppt
IBMStruts2培训PPT (大全集) 主讲:刘雷 完整实用 第一章:认识体验Struts2.ppt 第二章:struts2架构剖析.ppt ...第八章:struts2文件上传下载.ppt 第九章:struts2拦截器.ppt + Struts2开发详解.ppt
在struts2基础上实现的包括单文件、多文件的上传及下载
实现文件上传功能,并将文件数据保存到数据库中。 博文链接:https://chinalzd.iteye.com/blog/405509
Struts2上传文件(直接用request)
Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码
项目:Struts2UpAndDownLoadFile 1.实现文件的上传和下载 项目:Struts2HandleExcelFile 1.实现文件的上传和下载 2.利用apache的poi-3.5-FINAL-20090928.jar组件实现创建Excel表格。
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传
Struts 2.1.8 学习源码内容 Struts2_01FirstDemo : 跑通第一个Struts2的实例 Struts2_02CURD : 关于Struts2的增、删、改和查 实际业务中数据来自数据库,从DAO层查询...Struts2_06FileUpload : Struts2上传文件的使用
struts2上传文件进度条显示,里面有详细的代码
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
commons-fileupload-1.2.1.jar:文件上传组件,2.1.6版本后必须加入此文件 commons-io-1.3.2.jar freemarker-2.3.15.jar:Struts 2的UI标签的模板 ognl-2.7.3.jar :对象图导航语言(Object Graph Navigation ...
这套源码从最基本的JSP页面到Model 1到Model 2,文件上传,EL,JSTL,Ajax,Structs,一点一点的修改讲解。 最后形成一个完整的案例,比较经典。喜欢的朋友可以下载。以下是压缩文件的内容: 〖第17章:Struts高级...
Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
Struts2最新版本2.2.1中所设计的核心库文件,包括以下内容: struts2-core-2.2.1.jar:Struts2框架的核心类库。 xwork-core-2.2.1.jar:Xwork核心类库,Struts2在其上构建。 ognl-3.0.jar:对象图导航语言(Object ...
使用struts2框架的一个文件上传demo。。大家互相学习学习,里面有注释了,觉得好的顶一下吧。