struts2实现文件的上传下载

实现原理

Struts 2是通过Commons FileUpload文件上传。

Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件
具体实现:

一、创建index.jsp页面
复制代码

1
2
3


4
5
6
10
11
12
13
14
15
选择上传文件
7
8
9

16
17
18
19
22
23
测试.txt
20 测试.txt ”>下载
21

24
25

复制代码

  创建result.jsp页面
复制代码

1
2
3

成功上传的文件:
4

  • 5
    6
  • 7
    8

9

10
11

复制代码

当然别忘了在每个页面上都添加上struts2 标签引用<%@taglib prefix=”s” uri=”/struts-tags” %>

二、创建updown.js文件,在index.jsp中引用
复制代码

1 function checkf(){
2 var files = document.getElementsByName(“file”);
3 if(files[0].value.length!=0){
4 return true;
5 }else{
6 alert(“请选择文件”);
7 return false;
8 }
9 }
10 function addMore()
11 {
12 var td = document.getElementById(“more”);
13 var br = document.createElement(“br”);
14 var input = document.createElement(“input”);
15 var button = document.createElement(“input”);
16 input.type = “file”;
17 input.name = “file”;
18 button.type = “button”;
19 button.value = “Remove”;
20 button.onclick = function()
21 {
22 td.removeChild(br);
23 td.removeChild(input);
24 td.removeChild(button);
25 }
26 td.appendChild(br);
27 td.appendChild(input);
28 td.appendChild(button);
29 }

复制代码

三、创建upDownloadAction.java
复制代码

1 package com.action;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.io.UnsupportedEncodingException;
10 import java.util.List;
11 import javax.servlet.http.HttpServletRequest;
14 import com.opensymphony.xwork2.ActionSupport;
15 import org.apache.struts2.ServletActionContext;
16
17 public class UpDownloadAction extends ActionSupport {
18
19 private static final long serialVersionUID = 1L;
20 private List file;// 对应jsp中file标签
21 private List fileFileName;//
22 private List fileContentType;//
23 private String fileName;//获得jsp中pram参数
24 @SuppressWarnings(“deprecation”)
25 // 文件上传
26 public String uploadFiile() throws Exception {
27 String root = ServletActionContext.getServletContext().getRealPath(
28 “/upload”);// 上传路径
29 System.out.println(root);
30 InputStream inputStream;
31 File destFile;
32 OutputStream os;
33 for (int i = 0; i < file.size(); i++) {
34 inputStream = new FileInputStream(file.get(i));
35 destFile = new File(root, this.getFileFileName().get(i));
36 os = new FileOutputStream(destFile);
37 byte[] buffer = new byte[400];
38 int length = 0;
39 while ((length = inputStream.read(buffer)) > 0) {
40 os.write(buffer, 0, length);
41 }
42 inputStream.close();
43 os.close();
44 }
45 HttpServletRequest request = ServletActionContext.getRequest();
46 request.setAttribute(“fileName”, fileFileName);
47 return SUCCESS;
48 }
49
50 // 文件下载
51 public InputStream getDownloadFile() throws FileNotFoundException,
52 UnsupportedEncodingException {
53 System.out.println(getFileName());
54
55 // 如果下载文件名为中文,进行字符编码转换
56 ServletActionContext.getResponse().setHeader(“Content-Disposition”,”attachment;fileName=”
57 + java.net.URLEncoder.encode(fileName, “UTF-8”));
58 InputStream inputStream = new FileInputStream(“F:/” //使用绝对路径 ,从该路径下载“测试.txt”文件
59 + this.getFileName());
60 System.out.println(inputStream);
61 return inputStream;
62 }
63
64 // 下载
65 public String downloadFile() throws Exception {
66 return SUCCESS;
67 }
68
69 public String getFileName() throws UnsupportedEncodingException {
70 return fileName;
71 }
72
73 public void setFileName(String fileName)
74 throws UnsupportedEncodingException {
75 this.fileName = new String(fileName.getBytes(“ISO8859-1”), “utf-8”);
76 }
77 }

复制代码

注:属性的 get和set方法已省略。

四、最后是配置文件

1、web.xml配置
复制代码

1
2 struts2
3 org.apache.struts2.dispatcher.FilterDispatcher
4
5
6
7 struts2
8 /*
9

复制代码

2、struts.xml配置
复制代码

1
2
3
4
5
6 /jsp/result.jsp
7
8
10 409600
11
13
14 text/plain
15
16
17
18
19
20
21
22
23 application/txt;
24
25
26
28
29 attachment;filename=”${fileName}”
30
31
32 downloadFile
33
34 2048
35
36
37
38

复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值