先谈一谈struts2实现文件的上传和下载实例实现的原理:
Struts 2是通过Commons FileUpload文件上传。
Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。
具体实现:
一、创建index.jsp页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
< body >
< s:form action = "upload" method = "post" theme = "simple" enctype = "multipart/form-data" >
< table align = "center" width = "50%" border = "1" >
< tr >
< td >选择上传文件</ td >
< td id = "more" >
< s:file name = "file" ></ s:file >
< input type = "button" value = "Add More.." onclick = "addMore()" >
</ td >
</ tr >
< tr >
< td >< s:submit type = "button" value = "submit" onclick = "return checkf()" /></ td >
< td >< s:reset value = "reset " ></ s:reset ></ td >
</ tr >
</ table >
< table align = "center" width = "50%" border = "1" >
< tr >
< td >测试.txt</ td >
< td >
< a href="<s:url value = 'download.action' >< s:param name = 'fileName' >测试.txt</ s:param > </ s:url >">下载</ a >
</ td >
</ tr >
</ table >
</ s:form >
</ body >
|
创建result.jsp页面
1
2
3
4
5
6
7
8
9
10
11
|
< body >
< s:form >
< div style = "border:1px solid black" >成功上传的文件:< br >
< ul style = "list-style-type:decimal" >
< s:iterator value = "#request.fileName" id = "file" status = "status" >
< li >< s:property /> </ li >
</ s:iterator >
</ ul >
</ div >
</ s:form >
</ body >
|
当然别忘了在每个页面上都添加上struts2 标签引用<%@taglib prefix="s" uri="/struts-tags" %>
二、创建updown.js文件,在index.jsp中引用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
function checkf(){
var files = document.getElementsByName( "file" );
if (files[ 0 ].value.length!= 0 ){
return true ;
} else {
alert( "请选择文件" );
return false ;
}
}
function addMore()
{
var td = document.getElementById( "more" );
var br = document.createElement( "br" );
var input = document.createElement( "input" );
var button = document.createElement( "input" );
input.type = "file" ;
input.name = "file" ;
button.type = "button" ;
button.value = "Remove" ;
button.onclick = function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
|
三、创建upDownloadAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
public class UpDownloadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private List<File> file; // 对应jsp中file标签
private List<String> fileFileName; //
private List<String> fileContentType; //
private String fileName; //获得jsp中pram参数
@SuppressWarnings ( "deprecation" )
// 文件上传
public String uploadFiile() throws Exception {
String root = ServletActionContext.getServletContext().getRealPath(
"/upload" ); // 上传路径
System.out.println(root);
InputStream inputStream;
File destFile;
OutputStream os;
for ( int i = 0 ; i < file.size(); i++) {
inputStream = new FileInputStream(file.get(i));
destFile = new File(root, this .getFileFileName().get(i));
os = new FileOutputStream(destFile);
byte [] buffer = new byte [ 400 ];
int length = 0 ;
while ((length = inputStream.read(buffer)) > 0 ) {
os.write(buffer, 0 , length);
}
inputStream.close();
os.close();
}
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute( "fileName" , fileFileName);
return SUCCESS;
}
// 文件下载
public InputStream getDownloadFile() throws FileNotFoundException,
UnsupportedEncodingException {
System.out.println(getFileName());
// 如果下载文件名为中文,进行字符编码转换
ServletActionContext.getResponse().setHeader( "Content-Disposition" , "attachment;fileName="
+ java.net.URLEncoder.encode(fileName, "UTF-8" ));
InputStream inputStream = new FileInputStream( "F:/" //使用绝对路径 ,从该路径下载“测试.txt"文件
+ this .getFileName());
System.out.println(inputStream);
return inputStream;
}
// 下载
public String downloadFile() throws Exception {
return SUCCESS;
}
public String getFileName() throws UnsupportedEncodingException {
return fileName;
}
public void setFileName(String fileName)
throws UnsupportedEncodingException {
this .fileName = new String(fileName.getBytes( "ISO8859-1" ), "utf-8" );
}
}
|
注:属性的 get和set方法已省略。
四、最后是配置文件
1、web.xml配置
1
2
3
4
5
6
7
8
9
|
< filter >
< filter-name >struts2</ filter-name >
< filter-class >org.apache.struts2.dispatcher.FilterDispatcher</ filter-class >
</ filter >
< filter-mapping >
< filter-name >struts2</ filter-name >
< url-pattern >/*</ url-pattern >
</ filter-mapping >
|
2、struts.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
< struts >
< constant name = "struts.i18n.encoding" value = "utf-8" ></ constant >
< constant name = "struts.multipart.saveDir" value = "f:\" ></ constant >
< package name = "struts2" extends = "struts-default" >
< action name = "upload" class = "com.action.UpDownloadAction" method = "uploadFiile" >
< result name = "success" >/jsp/result.jsp</ result >
< interceptor-ref name = "fileUpload" >
<!--maximumSize (可选) - 这个拦截器允许的上传到action中的文件最大长度(以byte为单位).
注意这个参数和在webwork.properties中定义的属性没有关系,默认2MB-->
< param name = "maximumSize" >409600</ param >
<!--allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html),
这些列表是这个拦截器允许的可以传到action中的contentType.如果没有指定就是允许任何上传类型.-->
< param name = "allowedTypes" >
text/plain
</ param >
</ interceptor-ref >
< interceptor-ref name = "defaultStack" ></ interceptor-ref >
</ action >
< action name = "download" class = "com.action.UpDownloadAction" method = "downloadFile" >
< result name = "success" type = "stream" >
<!--指定文件下载类型 application/octet-stream默认值可以下载所有类型 -->
< param name = "contentType" >
application/txt;
</ param >
<!-- 指定下载的文件名和显示方式 ,但注意中文名的乱码问题,解决办法是:进行编码处理-->
<!--contentDisposition是文件下载的处理方式,包括内联(inline)和附件(attachment),
默认是inline, 使用附件时这样配置:attachment;filename="文件名" 。-->
< param name = "contentDisposition" >
attachment;filename="${fileName}"
</ param >
<!--由getDownloadFile()方法获得inputStream-->
< param name = "inputName" >downloadFile</ param >
<!-- 指定下载文件的缓存大小-->
< param name = "bufferSize" >2048</ param >
</ result >
</ action >
</ package >
</ struts >
|
一个简单的Struts2多文件上传和单文件下载就实现了。
以上就是本文的全部内容,希望对大家的学习有所帮助。