struts2学习(14)struts2文件上传和下载(4)多个文件上传和下载
四、多个文件上传:
五、struts2文件下载:


多个文件上传action
com.cy.action.FilesUploadAction.java:
package com.cy.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FilesUploadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private File[] struts; //文件,对应fileupload.jsp中input type=file的name
private String[] strutsFileName; //文件名
private String[] strutsContentType; //文件类型
public File[] getStruts() {
return struts;
}
public void setStruts(File[] struts) {
this.struts = struts;
}
public String[] getStrutsFileName() {
return strutsFileName;
}
public void setStrutsFileName(String[] strutsFileName) {
this.strutsFileName = strutsFileName;
}
public String[] getStrutsContentType() {
return strutsContentType;
}
public void setStrutsContentType(String[] strutsContentType) {
this.strutsContentType = strutsContentType;
}
@Override
public String execute() throws Exception {
for(int i=0; i<struts.length; i++){
System.out.println("文件名:" + strutsFileName[i]);
System.out.println("文件类型:" + strutsContentType[i]);
String filePath = "E:/" + strutsFileName[i];
File savefile = new File(filePath);
FileUtils.copyFile(struts[i], savefile);
}
return SUCCESS;
}
}
文件下载action
FileDownloadAction.java:
package com.cy.action; import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class FileDownloadAction extends ActionSupport{ private static final long serialVersionUID = 1L;
private String fileName; //下载的文件名 public String getFileName() throws Exception {
fileName=new String(fileName.getBytes(),"ISO8859-1"); //对文件中文名进行重编码
return fileName;
}
public void setFileName(String fileName){
this.fileName = fileName;
} /**
* 返回的是文件流
* @return
* @throws Exception
*/
public InputStream getInputStream() throws Exception{
File file = new File("E:/1.jpg"); //将E盘下的1.png下载下来
this.fileName = "美女哇哇.png"; //重新定义文件名
return new FileInputStream(file); //返回inputStream
}
}
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> <!-- 配置允许上传文件最大为20000000Byte,约20Mb -->
<constant name="struts.multipart.maxSize" value="20000000"></constant> <package name="manage" extends="struts-default">
<action name="upload" class="com.cy.action.FileUploadAction">
<result name="input">fileupload.jsp</result>
<result name="success">success.jsp</result> <!-- 配置允许上传的文件类型
配置允许上传的文件大小,最大81101byte,= 79.2KB
-->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
<param name="maximumSize">81101</param>
</interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref>
</action> <action name="uploads" class="com.cy.action.FilesUploadAction">
<result name="input">filesupload.jsp</result>
<result name="success">success.jsp</result>
</action> <action name="download" class="com.cy.action.FileDownloadAction">
<result type="stream">
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action>
</package> </struts>
多个文件上传filesupload.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>Insert title here</title>
</head>
<body>
<form action="uploads" method="post" enctype="multipart/form-data">
选择文件1:<input type="file" name="struts" /><br/>
选择文件2:<input type="file" name="struts" /><br/>
选择文件3:<input type="file" name="struts" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
文件上传成功success.jsp:
<body>
文件上传成功!
</body>
文件下载filedownload.jsp:
<body>
<a href="download">文件下载</a>
</body>
测试结果:
1.多个文件上传:

选择文件上传,成功后console打印:

2.文件下载,将E盘下的1.jpg下载下来:

struts2学习(14)struts2文件上传和下载(4)多个文件上传和下载的更多相关文章
- Struts2学习一----------Struts2的工作原理及HelloWorld简单实现
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...
- struts2学习(13)struts2文件上传和下载(1)
一.Struts2文件上传: 二.配置文件的大小以及允许上传的文件类型: 三.大文件上传: 如果不配置上传文件的大小,struts2默认允许上传文件最大为2M: 2097152Byte: 例子实现 ...
- Struts2学习笔记——Struts2与Spring整合
Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的 ...
- struts2学习(2)struts2核心知识
一.Struts2 get/set 自动获取/设置数据 根据上一章.中的源码继续. HelloWorldAction.java中private String name,自动获取/设置name: pac ...
- struts2学习(12)struts2验证框架2.自定义验证
一.例子需求: 对敏感词进行验证: 将struts包中的validators.xml文件拷贝一份到src目录下,在最后面添加自己的验证器: com.cy.validators.SensitiveWor ...
- struts2学习(10)struts2国际化
一.国际化简介: 二.struts2国际化设置: struts.xml: <?xml version="1.0" encoding="UTF-8" ?&g ...
- struts2学习(3)struts2核心知识II
一.struts.xml配置: 1.分模块配置方法: 比如某个系统多个模块,我们把资产管理模块和车辆管理模块,分开,在总的struts.xml配置文件中include他们: 工程结构: struts. ...
- Struts2学习笔记——Struts2搭建和第一个小程序
1.新建web项目 2.配置Struts2核心过滤器 (1)打开web.xml文件,做以下配置: <?xml version="1.0" encoding="UTF ...
- Struts2学习笔记--Struts2的体系结构
1. Struts2体系结构 Struts是以前端控制器框架为主体的框架,用户的请求会通过控制器选择不同的Action类来执行具体的操作,在Action类中所有的Servlet对象(request.r ...
随机推荐
- 部署C# ReportViewer遇到的坑
前些天临时给客户做个工具,统计具体时间点各种车型数据的数量及比重,为了显示方便就用C#来做,因为它有现成的reportviwer控件提供了显示,打印,导出功能.原本我以为这个控件是.netframew ...
- GSpan-频繁子图挖掘算法
GSpan频繁子图挖掘算法,网上有很多相关的介绍,中文的一些资料总是似是而非,讲的不是很清楚(感觉都是互相抄来抄去,,,基本都是一个样,,,),仔细的研读了原论文后,在这里做一个总结. 1. GSpa ...
- LaTex中插入大括号的多行公式
由于近期要发表论文,不得不恶补LaTex.现在需要插入带大括号的多行公式,效果如下: LaTex编辑如下: \begin{equation} \label{eq6} [x_{i}]=\left\{ \ ...
- Android 你可能忽略的提高敲代码效率的方式
Android 你可能忽略的提高敲代码效率的方式
- Jira简单使用操作指引20150605
1.选择项目 2.点击[问题]——>[所有问题] 3.选择状态(一般开发关注[新增.处理中],测试关注[已解决.已作废]) 4.选择[more],勾选[解决版本].[影响版本].[解决人],我们 ...
- json XML 比较
JSON: 这个为什么会变成“cc”而不是d.substring(dot+1);的值? 解决: var jsonsub = {}; jsonsub[cc] = e; arrnew.push(js ...
- crm 02--->讲师页面及逻辑
要求: 讲师 批量初始化 考勤 录入成绩 批量初始化 考勤与批量初始化这两个功能都要放在课程记录表中CourseRecord # 批量初始化 # 将该班的所有学生,初始化带某一天,而不是将每个学生一个 ...
- 单独编译某个pas文件
默认的询问.提示.警告框都是英文,找到Vcl.consts.pas改了下,重新编译,放到安装目录下,替换原有的即可. 1. 使用dcc32.exe编译指定的pas文件,dcc32.exe所在目录见下图 ...
- 压力测试工具ab及centos下单独安装方法
压力测试工具Ab简介 Apache安装包中自带的压力测试工具 Apache Benchmark(简称ab) 简单易用,这里就采用 ab作为压力测试工具了. 1.独立安装 ab运行需要依赖apr-uti ...
- Vim:基础
Normal模式 即是command “vim test.txt” 时进入的界面. 常用command: :help<Enter> 查看命令. :wq<Enter> ...