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 ...
随机推荐
- bzoj2501
题解: 显然,每当进入一个小的边界,那么我们的ans+1,出去一个大的边界,ans-1 然后,我们将每一个边界排序,时间小的在前,大的在后 每一次进来一个,如果是左边的边界,+1,右边的-1 然后输出 ...
- Python3的基础语法(四)
1,编码 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -* ...
- LINUX系统下的shell命令---diff、cut、sort、uniq等
1)diff:比较两个文件或目录的不同 -b 不检查空格字符的不同 -B 不检查空白行 -c 显示全部内容,并标出不同之处 -i 不检查 ...
- LINUX系统中高级网络服务:Bond、Team和网桥
Bond 一.什么是Bond bond就是将两块网卡虚拟链接成一块网卡的技术.通过bond技术让多块网卡看起来是一个单独的以太网接口设备并具有相同的ip地址. 二.为什么要配置Bond 通过网卡绑定技 ...
- Express 开发与部署最佳实践 -- 待续
链接 nginx 代理缓存 压缩 等 全部采用异步 使用try catch 处理同步异常 promise 处理异步 异常, 而不是使用 domains 或者 uncaughtExceptio ...
- three.js入门系列之光和阴影
初中物理教过我们镜面反射和漫反射,这是由于物体的材质直接导致的. 在three.js中,由于物体的材料不同,对于光源的反应也是不一样的,下面就让我们一探究竟. 一.材料 据Three.js中描述,有两 ...
- boost库之udp广播实例
//UdpLinkServer.h //udp服务 #pragma once #include <boost/asio/ip/tcp.hpp> #include <boost/asi ...
- 解决WIFI驱动RTL8188无法在rk3168平板Android4.2启动wifi的问题
http://blog.csdn.net/morixinguan/article/details/75228335 上一篇博文能把ko编译出来,非常兴奋的想,这一定是没问题了,结果删除原先的ko后,加 ...
- Machine Learning|Andrew Ng|Coursera 吴恩达机器学习笔记(完结)
Week 1: Machine Learning: A computer program is said to learn from experience E with respect to some ...
- python的基础
一. print(1 or 1 > 4) # 1 (从左到右1为True就结束了) print(1 > 1 or 3) # 3print(3 > 1 or 3 or 3 > ...