Struts2(五)
以下内容是基于导入struts2-2.3.32.jar包来讲的
1.文件上传
A.单文件上传
<body>
<form action="${pageContext.request.contextPath }/one" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</body>
 package com.rong.web.action;
 import java.io.File;
 import org.apache.commons.io.FileUtils;
 import com.opensymphony.xwork2.ActionSupport;
 public class MyAction extends ActionSupport {
     private static final long serialVersionUID = -351587239525292420L;
     //保存上传的文件对象,file对应表单元素名称,名称必须一致,拦截器会解析这个格式!
     private    File file;
     //文件名,必须为fileFileName
    //FileName 固定的写法。必须为file+FileName
     private String fileFileName;
     //ContentType 固定的写法。必须为file+ContentType
     private String fileContentType;
     public File getFile() {
         return file;
     }
     public void setFile(File file) {
         this.file = file;
     }
     public String getFileFileName() {
         return fileFileName;
     }
     public void setFileFileName(String fileFileName) {
         this.fileFileName = fileFileName;
     }
     public String getFileContentType() {
         return fileContentType;
     }
     public void setFileContentType(String fileContentType) {
         this.fileContentType = fileContentType;
     }
     @Override
     public String execute() throws Exception {
         //E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\ upload_1742945b_24ed_4612_a2f4_b83cffa61620_00000000.tmp
         //struts会保存到Tomcat服务器的struts2文件夹中创建临时文件,若不处理,执行完代码会把此临时文件删除
         System.out.println(file.getAbsolutePath());
         //butterfly.jpg
         System.out.println(fileFileName);
         //image/png
         System.out.println(fileContentType);
         File targetFile=new File("c:/", fileFileName);
         FileUtils.copyFile(file, targetFile);
         return SUCCESS;
     }
 }
文件类型: mime-type想了解的可以去Tomcat服务器的web.xml文件查看,里面有文件类型配置
E:\apache-tomcat-7.0.82\conf\web.xml
限制文件上传大小:
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (81498) exceeds the configured maximum (1024)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置限制文件上传大小为1K -->
<constant name="struts.multipart.maxSize" value="1024"></constant>
<package name="default" namespace="/" extends="struts-default" > <global-results>
<!-- struts在文件上传失败的时候,会返回input的错误视图,在这里配置对应的页面! -->
<result name="input">/input.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="one" class="com.rong.web.action.MyAction">
<result>/one.jsp</result>
</action>
</package>
</struts>
B.多文件上传
input的name属性值必须一致
<body>
<form action="${pageContext.request.contextPath }/one" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="file" name="file"/>
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</body>
 package com.rong.web.action;
 import java.io.File;
 import org.apache.commons.io.FileUtils;
 import com.opensymphony.xwork2.ActionSupport;
 public class MyAction extends ActionSupport {
     private static final long serialVersionUID = -351587239525292420L;
     //保存上传的文件对象,file对应表单元素名称,名称必须一致,拦截器会解析这个格式!
     private    File[] file;
     //文件名,必须为fileFileName
     private String[] fileFileName;
     //文件类型,必须为fileContentType
     private String[] fileContentType;
     public File[] getFile() {
         return file;
     }
     public void setFile(File[] file) {
         this.file = file;
     }
     public String[] getFileFileName() {
         return fileFileName;
     }
     public void setFileFileName(String[] fileFileName) {
         this.fileFileName = fileFileName;
     }
     public String[] getFileContentType() {
         return fileContentType;
     }
     public void setFileContentType(String[] fileContentType) {
         this.fileContentType = fileContentType;
     }
     @Override
     public String execute() throws Exception {
         if(file!=null){
             for(int i=0;i<file.length;i++){
                 System.out.println(file[i]);
                 System.out.println(fileFileName[i]);
                 System.out.println(fileContentType[i]);
                 File targetFile=new File("c:/", fileFileName[i]);
                 FileUtils.copyFile(file[i], targetFile);
             }
         }
         return SUCCESS;
     }
 }
E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\upload_ca4933df_aa32_4fc4_bbd5_d5d887c68972_00000006.tmp
2018届毕业设计2017-6-8.rar
application/octet-stream
E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\upload_ca4933df_aa32_4fc4_bbd5_d5d887c68972_00000007.tmp
nio1.png
image/png
E:\apache-tomcat-7.0.82\work\Catalina\localhost\struts2\upload_ca4933df_aa32_4fc4_bbd5_d5d887c68972_00000008.tmp
容杰龙.docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置限制文件上传大小为1000k -->
<constant name="struts.multipart.maxSize" value="10240000"></constant>
<package name="default" namespace="/" extends="struts-default" > <global-results>
<!-- struts在文件上传失败的时候,会返回input的错误视图,在这里配置对应的页面! -->
<result name="input">/input.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="one" class="com.rong.web.action.MyAction">
<result>/one.jsp</result>
</action>
</package>
</struts>
限制允许上传的文件类型以及文件扩展名:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置限制文件上传大小为1000k -->
<constant name="struts.multipart.maxSize" value="10240000"></constant>
<package name="default" namespace="/" extends="struts-default" > <global-results>
<!-- struts在文件上传失败的时候,会返回input的错误视图,在这里配置对应的页面! -->
<result name="input">/input.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="one" class="com.rong.web.action.MyAction">
<!-- Action里设置默认拦截器栈 -->
<interceptor-ref name="defaultStack">
<!-- 限制允许上传的文件类型 -->
<param name="fileUpload.allowedTypes">image/png,text/plain</param>
<!-- 限制上传文件的扩展名 -->
<param name="fileUpload.allowedExtensions">txt,png,docx</param>
<!-- 以上两个参数同时配置取交集 -->
</interceptor-ref>
<result>/one.jsp</result>
</action> </package>
</struts>
如果出现异常,上传失败,却无法弹出jsp页面,可在Tomcat服务器的server.xml文件中修改:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" maxSwallowSize="-1"/>
2.文件下载
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default" >
<action name="download" class="com.rong.web.action.MyAction">
<!-- 下载操作,name可以随意,但需要与返回值对应。type类型唯一。 -->
<result name="down" type="stream">
<!-- 文件的类型,指定为任意二进制类型,可以标识任何文件 -->
<param name="contentType">application/octet-stream</param>
<!-- 对应的是Action类中返回流的属性!对应getFileStream()方法 -->
<param name="inputName">fileStream</param>
<!-- 指定浏览器显示的文件名,对应action类中的返回文件名的属性!需要url编码,对应getDownFile()方法 -->
<param name="contentDisposition">attachment;filename=${downFile}</param>
<!-- 读取缓冲区大小 -->
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
package com.rong.web.action; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import com.opensymphony.xwork2.ActionSupport; public class MyAction extends ActionSupport {
private static final long serialVersionUID = -351587239525292420L;
private String filePath;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
@Override
public String execute() throws Exception {
//实际开发中,用户只要传文件路径即可实现下载,这里固定路径。
filePath="C:/2017.11.17-广州市部份培训公司名单.docx";
return "down";
}
//返回流<param name="inputName">fileStream</param>
public InputStream getFileStream() throws FileNotFoundException{
return new FileInputStream(filePath);
}
//<param name="contentDisposition">attachment;filename=${downFile}</param>
public String getDownFile() throws UnsupportedEncodingException{
File file=new File(filePath);
String name = file.getName();
return URLEncoder.encode(name, "UTF-8");
}
}
Struts2(五)的更多相关文章
- Struts2(五):ActionSupport
		
我们在上一章节中的一个列子中使用到了一个标识跳转到登录页面的例子: 示例是这样写的: index.jsp: <br/> <a href="gotoLoginPage&quo ...
 - Struts2(五)——核心拦截器
		
Struts框架一共为我们提供了35个拦截器,其中默认的拦截器有18个,框架访问action的异常处理,配置信息处理,转发重定向选择,上传等等等等,都是这18个拦截器中设置的,起着非比寻常的作用.而这 ...
 - Struts2五、Struts1与Struts2的区别
		
Struts1和Struts2的区别和对比: Action 类: • Struts1要求Action类继承一个抽象基类.Struts1的一个普遍问题是使用抽象类编程而不是接口,而struts2的Ac ...
 - Struts2(五)常量的配置
		
Struts2 常量大多在 默认的配置文件中已经配置好,但根据用户的需求不同,开发的要求不同,需要修改这些常量值,修改的方法就是在配置的文件对常量进行重新配置 在struts.xml 文件中使用< ...
 - struts2(五) s标签和国际化
		
坚持就是胜利. --WH 一.s标签 在struts-2.3.15.1/docs/WW/docs/tag-reference.html下,就有着struts2所有标签的参考文献,只能看看其中比较常用的 ...
 - Struts2(五)数据校验
		
一.概述 在提交表单数据时,如果数据需要保存到数据库,空输入等可能会引发一些异常,为了避免引起用户的输入引起底层异常,通常在进行业务逻辑操作之前,先执行基本的数据校验. 下面通过两种方式来阐述Stru ...
 - Struts2(五)Action二配置
		
一.method参数 action package com.pb.web.action; public class HourseAction { public String add(){ System ...
 - Struts2(五.用户注册的实现及整合Action的配置方法)
		
一.用户注册功能 register.jsp页面 若是jquery ajax方式提交给action,还要回到jquery,控制权在jquery若是表单方式提交给action,控制权交给action &l ...
 - 【Struts2五】ValueStack以及ognl表达式二(经常使用标签)
		
Ognl经常使用标签: 1.s:debug 假设把该标签放入到s:iterator中能够看到当前正在迭代的元素的状态 2.s:property 1.输出 ...
 - 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
		
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
 
随机推荐
- MQTT入门1 -- mosquitto 安装
			
原文链接:https://www.cnblogs.com/NickQ/p/9247638.html MQTT入门1 -- mosquitto 安装 简介: MQTT(Message Queuing T ...
 - Fiddler抓取手机APP数据包
			
第一步:下载神器Fiddler,下载链接: http://w.x.baidu.com/alading/anquan_soft_down_ub/10963 下载完成之后,傻瓜式的安装一下了! 第二步:设 ...
 - 如何通过审计安全事件日志检测密码喷洒(Password Spraying)攻击
			
许多渗透测试人员和攻击者通常都会使用一种被称为“密码喷洒(Password Spraying)”的技术来进行测试和攻击.对密码进行喷洒式的攻击,这个叫法很形象,因为它属于自动化密码猜测的一种.这种针对 ...
 - python2.7入门---GUI编程(Tkinter)
			
Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 . ...
 - 加分项——C语言实现Linux的pwd命令
			
加分项--C语言实现Linux的pwd命令 实现要求 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd pwd pw ...
 - 20155318 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
			
20155318 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 一.实验内容及步骤 (一)命令行下Java程序开发 步骤一(新建文件夹): 打开windows下的cmd → ...
 - sql语句-7-更新数据
 - [2016北京集训测试赛7]isn-[树状数组+dp+容斥]
			
Description Solution 定义dp[i][j]为在1到i个数中选了j个数,并且保证选了i的选法总数. dp[i][j]为所有满足A[k]>A[i]的k(k<i)的dp[k] ...
 - Jquery操作下拉列表和复选框,自定义下拉
			
后半部分还有自定义下拉列表和开灯关灯的效果,可以进来来看一下 哦 如果网页有下拉列表和复选框,看一下Jquery怎么来操作他们,主要怎么来选取他们的数据,怎么设置某一项选中 先来看个下拉列表 < ...
 - c# 实体类怎么给LIST赋值,table转LIST
			
/// <summary> /// 缓存客服集合信息 /// </summary> public class model { /// <summary> /// 客 ...