- 浏览: 126487 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
akushin:
是不是会少一行··
用apache poi 读取excel文件很方便 -
mady0505:
楼下的童鞋,如果有隔行,稍微判断一下就可以了
if(row!= ...
用apache poi 读取excel文件很方便 -
漫步林中:
学习了。谢谢
Struts2中的文件上传 -
cuisuqiang:
楼主是否发现int sheetNum=wb.getNumber ...
用apache poi 读取excel文件很方便 -
yaoneng:
谢谢楼主,弄明白了~~
用apache poi 读取excel文件很方便
struts2 文件上传
(1) 单文件上传
一.简介
Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来。但Struts2在原有的上传解析器基础上做了进一步封装,更进一步简化了文件上传。
Struts2默认使用的是Jakarta的Common-FileUpload框架来上传文件,因此,要在web应用中增加两个Jar文件:commons-fileupload-1.2.jar和commons-io-1.3.1.jar。它在原上传框架上做了进一步封装,简化了文件上传的代码实现,取消了不同上传框架上的编程差异。
如果要改成其它的文件上传框架,可以修改struts.multipart.parser常量的值为cos/pell,默认值是jakata。并在classpath中增加相应上传组件的类库
例如配置成cos上传
struts.multipart.parser=cos
struts.multipart.maxSize=1024 指定文件的最大字结数
二.原理
不管用common-fileUPload框架,还是用cos,都是通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。
三.需要的jar包(默认使用commons-fileupload,如果使用cos,要将jar引进来)
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.3.1.jar
commons-fileupload-1.2.jar
四.实例
1.首先,创建上传页面
Html代码
2.action
Java代码
表单的enctype ="multipart/form-data,与一般的上传一样.
<s:file name="upload">会将upload绑定到action的upload,其次他还会将上传记文件的MIME类型绑定到uploadContentType,文件名绑定到uploadFileName中,他们是通过
setUploadContentType和setUploadFileName进行绑定的,下面进行的多文件上传也是同个道理,不过要用数组或者是list来进行绑定,然后多个文件的MIME类型也会绑定到以数组
名字加ContentType和FileName的字符串数组中。 比如说上传的文件的数组名为:File[] uploads,则它们的MIME类型绑定的对应的数组是uploadsFileName和uploadsContentType.
3.struts.xml的配置
Xml代码
4.最后是web.xml的配置
Xml代码
(2) 多文件上传
多文件上传
与单文件上传相似,Struts 2实现多文件上传也很简单。你可以将多个<s:file />绑定Action的数组或列表。如下例所示。
< s:form action ="doMultipleUploadUsingList" method ="POST" enctype ="multipart/form-data" >
< s:file label ="File (1)" name ="upload" />
< s:file label ="File (2)" name ="upload" />
< s:file label ="FIle (3)" name ="upload" />
< s:submit />
</ s:form > 清单14 多文件上传JSP代码片段
如果你希望绑定到数组,Action的代码应类似:
private File[] uploads;
private String[] uploadSFileName;
private String[] uploadSContentType;
多文件上传数组绑定Action代码片段
如果你想绑定到列表,则应类似:
private List < File > uploads = new ArrayList < File > ();
private List < String > uploadSFileName = new ArrayList < String > ();
private List < String > uploadSContentType = new ArrayList < String > ();
多文件上传列表绑定Action代码片段
总结
在Struts 2中实现文件上传的确是轻而易举,您要做的只是使用<s:file />与Action的属性绑定。这又一次有力地证明了Struts 2的简单易用。
(1) 单文件上传
一.简介
Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来。但Struts2在原有的上传解析器基础上做了进一步封装,更进一步简化了文件上传。
Struts2默认使用的是Jakarta的Common-FileUpload框架来上传文件,因此,要在web应用中增加两个Jar文件:commons-fileupload-1.2.jar和commons-io-1.3.1.jar。它在原上传框架上做了进一步封装,简化了文件上传的代码实现,取消了不同上传框架上的编程差异。
如果要改成其它的文件上传框架,可以修改struts.multipart.parser常量的值为cos/pell,默认值是jakata。并在classpath中增加相应上传组件的类库
例如配置成cos上传
struts.multipart.parser=cos
struts.multipart.maxSize=1024 指定文件的最大字结数
二.原理
不管用common-fileUPload框架,还是用cos,都是通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。
三.需要的jar包(默认使用commons-fileupload,如果使用cos,要将jar引进来)
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.3.1.jar
commons-fileupload-1.2.jar
四.实例
1.首先,创建上传页面
Html代码
<%@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"> <html> <head> <title>My JSP 'FileUpload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data"> <s:fielderror /> <s:file name ="upload"/> <s:submit /> </s:form > </body> </html>
<%@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"> <html> <head> <title>My JSP 'FileUpload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data"> <s:fielderror /> <s:file name ="upload"/> <s:submit /> </s:form > </body> </html>
2.action
Java代码
package com; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Date; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport { private static final long serialVersionUID = 572146812454l; private File upload; private String uploadContentType; private String uploadFileName; private String imageFileName; public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } 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 void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; } public String getImageFileName() { return imageFileName; } private static void copy(File src, File dst) { try { InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src)); out = new BufferedOutputStream(new FileOutputStream(dst)); byte[] buffer = new byte[1024*10]; while (in.read(buffer) > 0) { out.write(buffer); } } finally { if (null != in) { in.close(); } if (null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } } @Override public String execute() { System.out.println(uploadFileName); imageFileName = System.currentTimeMillis() + uploadFileName.substring(uploadFileName.lastIndexOf(".")); File imageFile = new File(ServletActionContext.getServletContext() .getRealPath("/uploadImages") + "/" + imageFileName); copy(upload, imageFile); return SUCCESS; } } package com; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Date; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport { private static final long serialVersionUID = 572146812454l; private File upload; private String uploadContentType; private String uploadFileName; private String imageFileName; public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } 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 void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; } public String getImageFileName() { return imageFileName; } private static void copy(File src, File dst) { try { InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src)); out = new BufferedOutputStream(new FileOutputStream(dst)); byte[] buffer = new byte[1024*10]; while (in.read(buffer) > 0) { out.write(buffer); } } finally { if (null != in) { in.close(); } if (null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } } @Override public String execute() { System.out.println(uploadFileName); imageFileName = System.currentTimeMillis() + uploadFileName.substring(uploadFileName.lastIndexOf(".")); File imageFile = new File(ServletActionContext.getServletContext() .getRealPath("/uploadImages") + "/" + imageFileName); copy(upload, imageFile); return SUCCESS; } }
表单的enctype ="multipart/form-data,与一般的上传一样.
<s:file name="upload">会将upload绑定到action的upload,其次他还会将上传记文件的MIME类型绑定到uploadContentType,文件名绑定到uploadFileName中,他们是通过
setUploadContentType和setUploadFileName进行绑定的,下面进行的多文件上传也是同个道理,不过要用数组或者是list来进行绑定,然后多个文件的MIME类型也会绑定到以数组
名字加ContentType和FileName的字符串数组中。 比如说上传的文件的数组名为:File[] uploads,则它们的MIME类型绑定的对应的数组是uploadsFileName和uploadsContentType.
3.struts.xml的配置
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="/tmp" /> <package name="fileUploadDemo" extends="struts-default"> <action name ="fileUpload" class ="com.FileUploadAction"> <!-- 验证上传文件的类型 --> <interceptor-ref name ="fileUpload"> <param name ="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> </interceptor-ref> <interceptor-ref name ="defaultStack"/> <!-- 如果加入了验证上传文件的类型,必须要加input --> <result name ="input" >/fileUpload.jsp</result> <result name ="success">/showUpload.jsp </result> </action> </package> </struts> <?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="/tmp" /> <package name="fileUploadDemo" extends="struts-default"> <action name ="fileUpload" class ="com.FileUploadAction"> <!-- 验证上传文件的类型 --> <interceptor-ref name ="fileUpload"> <param name ="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> </interceptor-ref> <interceptor-ref name ="defaultStack"/> <!-- 如果加入了验证上传文件的类型,必须要加input --> <result name ="input" >/fileUpload.jsp</result> <result name ="success">/showUpload.jsp </result> </action> </package> </struts>
4.最后是web.xml的配置
Xml代码
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>struts-cleanup</filter-name> <filter-class> org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> </filter> <filter> <filter-name>struts2</filter-name > <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class > </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name > <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name > <url-pattern>/*</url-pattern > </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
(2) 多文件上传
多文件上传
与单文件上传相似,Struts 2实现多文件上传也很简单。你可以将多个<s:file />绑定Action的数组或列表。如下例所示。
< s:form action ="doMultipleUploadUsingList" method ="POST" enctype ="multipart/form-data" >
< s:file label ="File (1)" name ="upload" />
< s:file label ="File (2)" name ="upload" />
< s:file label ="FIle (3)" name ="upload" />
< s:submit />
</ s:form > 清单14 多文件上传JSP代码片段
如果你希望绑定到数组,Action的代码应类似:
private File[] uploads;
private String[] uploadSFileName;
private String[] uploadSContentType;
多文件上传数组绑定Action代码片段
如果你想绑定到列表,则应类似:
private List < File > uploads = new ArrayList < File > ();
private List < String > uploadSFileName = new ArrayList < String > ();
private List < String > uploadSContentType = new ArrayList < String > ();
多文件上传列表绑定Action代码片段
总结
在Struts 2中实现文件上传的确是轻而易举,您要做的只是使用<s:file />与Action的属性绑定。这又一次有力地证明了Struts 2的简单易用。
发表评论
-
spring里quartz调度的连续触发
2008-11-05 17:08 2325作法是通过spring在执行的触发器的监听器的里面注入下 ... -
web-harvest基础学习一
2008-10-15 11:50 3212Web-harvest里面的<script>标 ... -
使用web-harvest抓取分页数据(二)
2008-10-14 16:40 3403除了使用上述方法外,还可以使用将不页的数据输出到不同的结 ... -
使用Web-Harvest抓取分页的数据
2008-10-14 14:07 4565可以利用其自带的例子里面已经写好了的那个函数,在Func ... -
使用Web-Harvest抓取数据并保存到数据库(一)
2008-10-13 12:23 56731. 从其官网:http://web-harvest.sour ... -
用cactus测试servlet(三)
2008-09-09 18:00 12111. 基于测试方法的名字,ServletTestCase将在 ... -
用cactus测试servlet(二)
2008-09-09 17:58 13121. b ... -
html截取摘要并补齐标签(htmlparser)二
2008-08-26 11:09 3292另外一个关键点就在于补齐截取出来的标签,而在这之前你可能需要面 ... -
JAVA常见的异常(收藏)
2008-08-01 12:26 2279转载自: http://hi.baidu.co ... -
监听器ServletContextListener小记
2008-07-25 15:04 4285今天奉命写一个监听器监听tomcat,在tomcat启动 ... -
java 里的文件生成与删除
2008-07-10 16:33 1516这里先是文件的生成: import java.io.* ... -
用dom4j读写数据
2008-07-05 15:52 2144从数据库表里面读取数据并生成一个xml文件给用户下载,下 ... -
临时记录
2008-06-28 16:38 862http://www.blogjava.net/lusm/ar ... -
Struts里面容易出现乱码的地方及解决方法
2008-06-14 21:55 2318容易出现乱码的地方一般有三个地方: a.页面出现乱码 ... -
Struts与Velocity的简单集成
2008-06-12 17:00 1417首先当然是简单的配置web.xml与struts-config ... -
用apache poi 读取excel文件很方便
2008-06-07 00:26 12170用三个循环遍历就行了: 第一层是sheet循环,然后是行循环, ... -
顺序生成,无聊代码...
2008-06-06 00:22 1078package com.jie.java; public ...
相关推荐
struts2中文件上传过滤codeFilter
jspstruts1_2struts2 中文件上传 java文件上传
struts2中的文件上传和下载: 1.首先导入commons-fileupload-1.2.1.jar和commons-io-1.4.jar两个jar包 2.其次JSP页面中的文件上传控件所在的form必须设置属性enctype="multipart/form-data" 3.再次,struts2中...
在struts2基础上实现的包括单文件、多文件的上传及下载
Struts2中文件上传与下载案例
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传
Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
文件上传比较多,多文件上传少一点 文件下载很少的,看似简单,实则不然 网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供...
struts2中的文件上传和下载示例 struts2中的文件上传和下载示例 struts2中的文件上传和下载示例
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
一个swfupload+struts2实现多文件上传的案例。
struts2实现文件上传(单个+多个文件上传
struts实现的文件上传下载功能,最大可以上传到150MB
自己做的一个struts2 实现文件批量上传功能,
Struts2上传文件(直接用request)
基于struts2的文件上传+过滤器 可以动态的控制文件的类型和文件大小
在Struts 2中实现文件上传