Struts2的文件上传与下载
<一>简述:
Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<二>指定用户上传文件的大小,有两种方式:
1)默认是在default.properties 文件的 struts.multipart.maxSize=2097152 键值指定为2097152 也就是2M,通过计算 2097152/(1024*1024) = 2 M
那我们可以改变其默认值,只要在src目录下,新建一个 struts.properties 文件,指定上传大小 如下:

一次上传只可以上传10M,不管一次上传多少个文件,按总和计算
2)在struts.xml文件中指定,如图:

其实name就对应struts.properties的键,value对应 值
注意:如果即在struts.properties设定文件上传大小,又在struts.xml 设定文件上传大小,则struts.properties的优先级高于struts.xml,一般在一处指定上传大小即可,推荐 struts.properties
一、单文件上传
首先是一个jsp文件上传页面,这个比较简单,就是一个表单,里面有个文件上传框

<!--在进行文件上传时,表单提交方式一定要是post的方式,因为文件上传时二进制文件可能会很大,还有就是enctype属性,这个属性一定要写成multipart/form-data,
不然就会以二进制文本上传到服务器端-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>文件上传</h1>
<form action="Upload_upload" method="post" enctype="multipart/form-data">
标题:<input type="text" name="title"><br>
附件:<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>
</body>
</html>

接下来是UploadAction部分代码,因为struts2对上传和下载都提供了很好的实习机制,所以在action这段我们只需要写很少的代码就行:

public class UploadAction extends ActionSupport{
private String title;
//注意,file并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件
private File file;
//提交过来的file的名字
private String fileFileName;
//提交过来的file的MIME类型
private String fileContentType;
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String input(){
return SUCCESS;
}
public String upload() throws IOException{
//System.out.println(file.length());
//为了防止上传的文件重名,可以在文件前面加当前日期,随机数字,文件名称等
Random rand = new Random();
int n = rand.nextInt(9999);
DecimalFormat format = new DecimalFormat("0000");
String sss = format.format(n);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
Date now = Calendar.getInstance().getTime();
String prefix = sdf.format(now);
String fileName = prefix +"_"+ sss+"_" + fileFileName;
// System.out.println(fileFileName);
// System.out.println(fileContentType );
File destFile = new File("d:\\"+fileName);
FileUtils.copyFile(file, destFile);
return SUCCESS;
}
}

首先我们要清楚一点,这里的file并不是真正指代jsp上传过来的文件,当文件上传过来时,struts2首先会寻找struts.multipart.saveDir(这个是在default.properties里面有)这个name所指定的存放位置,我们可以新建一个struts.properties属性文件来指定这个临时文件存放位置,如果没有指定,那么文件会存放在tomcat的apache-tomcat-7.0.29\work\Catalina\localhost\目录下,然后我们可以指定文件上传后的存放位置,通过输出流将其写到流里面就行了,这时我们就可以在文件夹里看到我们上传的文件了。
Strurts.XML文件的配置

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.multipart.maxSize" value="20971520"></constant>
<package name="default" namespace="/" extends="struts-default">
<!--通过一个通用的Action就可以实现功能。下面的上传多个文件同理这个Action-->
<action name="*_*" class="com.itnba.maya.controller.{1}Action" method="{2}">
<result>{1}/{2}.jsp</result>
</action>
</package>
</struts>

struts2多文件上传:
其实多文件上传和单文件上传原理一样,单文件上传过去的是单一的File,多文件上传过去的就是一个List<File>集合或者是一个File[]数组,首先我们来看一下前端jsp部分的代码,这里我用到了jquery来实现动态的添加文件下载框以及动态的删除下载框:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>多文件上传</h1>
<form action="UploadMany_upload" method="post" enctype="multipart/form-data">
标题:<input type="text" name="title"><br>
附件1:<input type="file" name="file"><br>
附件2:<input type="file" name="file"><br>
附件3:<input type="file" name="file"><br>
附件4:<input type="file" name="file"><br>
附件5:<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>
</body>
</html>

file的名字必须都命名成file才行,然后处理多文件上传的action代码如下:

public class UploadManyAction extends ActionSupport{
private String title;
private File[] file;//这个位置必须都改成数组的形式
private String[] fileFileName;
private String[] fileContentType;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
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;
}
public String input(){
return SUCCESS;
}
public String upload() throws IOException{
for(int i=0;i<file.length;i++){ //用循环把数组遍历出来然后上传进去,这里的名称修改就不再列出,同理单个文件上传。
File item = file[i];
if(item != null){
File temp = new File("d:\\"+fileFileName[i]);
FileUtils.copyFile(item, temp);
}
}
return SUCCESS;
}
}

Sturts.XML文件和单个文件上传的一样。这样就可以通过Struts2上传文件了。
Struts2的文件上传与下载的更多相关文章
- struts2实现文件上传和下载
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...
- 【Struts2】文件上传与下载
一.上传 1.1 Struts2实现步骤 浏览器端 服务器端 1.2 关于Struts2中文件上传细节: 1.3 示例 jsp文件 Action类 struts.xml文件配置 二.下载 2.1 文件 ...
- (十五)struts2的文件上传和下载
文件上传的原理 我们以前学习上传的时候知道需要将表单的enctype属性设置为multipart/form-data. 表单的enctype属性指定的是表单数据的编码方式,有三个值: -applica ...
- Struts2之文件上传与下载
时间:2017-1-11 15:47 --使用commons-fileupload组件上传1.客户端 * method="post" * <input t ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
- Struts2入门(七)——Struts2的文件上传和下载
一.前言 在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载. 注意:文件上传时,我们需要将表单提交方式设置为"POST" ...
- Struts2 实现文件上传和下载
实现上传功能 Struts2实际上是使用的commons fileupload 组件,所以记得导包哦. 1.首先你应该有一个上传页面 <!-- file的name属性与action中的File类 ...
- 7、Struts2实现文件上传和下载
一.实现单个文件上传 1.创建如下web项目结构 2.在src下的com.action包下创建UploadAction.java package com.action; import java.io. ...
- struts2(六) 文件上传和下载
前面对文件下载提过一点点,这里正好要讲文件上传,就放在一起在说一遍. --WH 一.单文件上传 在没学struts2之前,我们要写文件上传,非常麻烦,需要手动一步步去获取表单中的各种属性,然后在进行相 ...
随机推荐
- SqlServer中嵌套事务使用--事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配 --根本问题
转自 :SqlServer中嵌套事务使用--事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配 --根本问题 问题: 1. System.Data.SqlClient.SqlExcepti ...
- HDU 2149 Public Sale 拍卖(巴什博弈)
思路:只要能给对方留下n+1,我就能胜,否则败. #include <iostream> #include <cstdio> using namespace std; int ...
- NGUI类之间的关系架构
NGUI Drawcall 1.使用同一个altals的元素尽量放在同一个UIPanel下面,在NGUI中,它消耗的drawcall是以每个Panel为独立计算单位进行计算的. 2.如果一个UIPan ...
- windows系统下使用.net简单操作redis
首先.net需要引入如下几个文件,在gitub或者官网应该是有的: 然后配置一下redis服务器: 端口: IP: 然后先启动 redis-server.exe: 出现如下效果表示成功 再启动:re ...
- HDU - 5096 ACM Rank (Treap)
平衡树的题,Treap破之,比较难搞的出现相同题数罚时的情况,解决方法是在每个结点用一个set, 保证结点值的时候可以把题数和罚时保存到一个int里,令v = n*MaxPenaltySum-pena ...
- python3.6.2利用pyinstaller发布EXE
我的环境是Ubuntu 16.04,系统自带Python2和Python3 安装 pip3 install pyinstaller 发布exe pyinstaller -F helloworld.py ...
- GitHub和码云的简单使用
年轻,又经历了初高大学的英语的纠缠,导致连最简单的语句都看不懂,我在慢慢寻找语言的快乐 GitHub 的简单使用 : https://www.cnblogs.com/zhcncn/p/3731707. ...
- iOS跳转到各种系统设置界面
定位服务 定位服务有很多APP都有,如果用户关闭了定位,那么,我们在APP里面可以提示用户打开定位服务.点击到设置界面设置,直接跳到定位服务设置界面.代码如下: //定位服务设置界面 NSURL *u ...
- VueX源码分析(1)
VueX源码分析(1) 文件架构如下 /module /plugins helpers.js index.esm.js index.js store.js util.js util.js 先从最简单的 ...
- MATLAB编程技巧
[摘要] MATLAB是一种科学计算语言,和C.Fortran等高级语言相类似,能方便的实现程序控制.以下介绍一点matlab编程的技巧. 嵌套计算 程序执行的速度取决于调用的子程序的个数和算法实现. ...