struts2中的文件上传和文件下载

                                      单文件文件上传

1.

uploadAction.java

 

 1 package cn.hmy.upload;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 /**
12  * 文件上传
13  * **/
14 public class uploadAction extends ActionSupport{
15 
16     //封装上传文件属性
17     private File upload;
18     //封装上传文件的类型
19     private String uploadContentType;
20     //封装上传文件的名称
21     private String uploadFileName;
22     //获取文件上传的路径
23     private String savePath;
24     
25     
26     public String upload() throws Exception {
27         byte[] buffer=new byte[1024];
28         //读取文件
29         FileInputStream fis=new FileInputStream(getUpload());
30         //保存文件,并设置保存目录的路径
31         FileOutputStream fos=new FileOutputStream(getSavePath()+"//"+this.getUploadFileName());
32         int length=fis.read(buffer);
33         while(length>0){
34             //每次写入length长度的内容
35             fos.write(buffer, 0, length);
36             length=fis.read(buffer);
37         }
38         fis.close();
39         fos.flush();
40         fos.close();
41         
42         return SUCCESS;
43     }
  
57     
58     public File getUpload() {
59         return upload;
60     }
61     public void setUpload(File upload) {
62         this.upload = upload;
63     }
64     public String getUploadContentType() {
65         return uploadContentType;
66     }
67     public void setUploadContentType(String uploadContentType) {
68         this.uploadContentType = uploadContentType;
69     }
70     public String getUploadFileName() {
71         return uploadFileName;
72     }
73     public void setUploadFileName(String uploadFileName) {
74         this.uploadFileName = uploadFileName;
75     }
76     
77     //获取文件上传的保存路径
78         //通过读取存放目录获得路径
79     public String getSavePath() {
80         
81         return ServletActionContext.getServletContext().getRealPath(savePath);
82     }
83     public void setSavePath(String savePath) {
84         this.savePath = savePath;
85     }
86     
87 }

upload.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6 <package name="upload" namespace="/upload" extends="struts-default">
 7   <action name="upload" class="cn.hmy.upload.uploadAction" method="upload">
 8     <param name="savePath">/images</param>
 9     <result name="success">/upload_success.jsp</result>
10   </action>
11 </package>
12 </struts>

struts.xml

upload.jsp

 

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <%@taglib uri="/struts-tags" prefix="s" %>
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>文件上传</title>
13     
14   </head>
15   
16   <body>
17     <s:form action="upload/upload" enctype="multipart/form-data" method="post">
18         
19         <s:label value="文件一:"/>&nbsp&nbsp<br/>
20         <s:url /><s:file name="upload" />
21         <%-- <s:file name="upload"></s:file><br/> --%>
22        <%--  <s:file name="upload" lable="选择文件"></s:file><br/>
23         <s:file name="upload" lable="选择文件"></s:file><br/> --%>
24         <s:submit name="submit" value="上传文件"></s:submit>
25     </s:form>
26     <s:debug></s:debug>
27   </body>
28 </html>

在地址栏请求

 

                                       多文件上传

多文件上传和单文件上传的配置是一样的,只是action有所区别

 1 package cn.hmy.doubleupload;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 /**
12  * 实现多文件上传
13  * **/
14 public class DoubleUpload extends ActionSupport {
15 
16     
17     //获取提交的多个文件
18     private File[] upload;
19     //封装上传文件的类型
20     private String[] uploadContentType;
21     //封装上传文件的类型
22     private String[] uploadFileName;
23     //文件上传路径
24     private String savePath;
25     
26     public String doubleUpload() throws Exception{
27         
28         byte[] buffer=new byte[1024];
29         for (int i = 0; i < upload.length; i++) {
30             FileInputStream fis=new FileInputStream(getUpload()[i]);
31             FileOutputStream fos=new FileOutputStream(getSavePath()+"//"+getUploadFileName()[i]);
32             int length=fis.read(buffer);
33             while(length>0){
34                 fos.write(buffer, 0, length);
35                 length=fis.read(buffer);
36             }
37             fis.close();
38             fos.flush();
39             fos.close();
40         }
41         
42         
43         return SUCCESS;
44     };
45     
46     
47     
48     
49     
50     
51     
52     
53     
54     
55     
56     public String getSavePath() {
57         return ServletActionContext.getServletContext().getRealPath(savePath);
58     }
59     public void setSavePath(String savePath) {
60         this.savePath = savePath;
61     }
62     public File[] getUpload() {
63         return upload;
64     }
65     public void setUpload(File[] upload) {
66         this.upload = upload;
67     }
68     public String[] getUploadContentType() {
69         return uploadContentType;
70     }
71     public void setUploadContentType(String[] uploadContentType) {
72         this.uploadContentType = uploadContentType;
73     }
74     public String[] getUploadFileName() {
75         return uploadFileName;
76     }
77     public void setUploadFileName(String[] uploadFileName) {
78         this.uploadFileName = uploadFileName;
79     }
80     
81     
82 }

 

 

                                          文件下载

downloadActiion.java

 1 package cn.hmy.download;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.InputStream;
 7 
 8 import org.apache.struts2.ServletActionContext;
 9 
10 import com.opensymphony.xwork2.ActionSupport;
11 
12 /**
13  * 下载文件
14  * **/
15 public class downloadAction extends ActionSupport{
16 
17     //读取下载文件的路径
18     private String inputPath;
19     //下载文件的文件名
20     private String filename;
21     //读取下载文件的文件流
22     private InputStream inputStream;
23     //下载文件的类型
24     private String contentType;
25     
26     //创建inputStream输入流
27     public InputStream getInputStream() throws FileNotFoundException{
28         String path=ServletActionContext.getServletContext().getRealPath(inputPath);
29         return new BufferedInputStream(new FileInputStream(path+"//"+filename));
30         
31         
32     }
33     
34     public String download() throws Exception{
35         return SUCCESS;
36     }
37     
38     
39     
40     
41     public String getInputPath() {
42         return inputPath;
43     }
44     public void setInputPath(String inputPath) {
45         this.inputPath = inputPath;
46     }
47     public String getFilename() {
48         return filename;
49     }
50     public void setFilename(String filename) {
51         this.filename = filename;
52     }
53     
54     public void setInputStream(InputStream inputStream) {
55         this.inputStream = inputStream;
56     }
57     public String getContentType() {
58         return contentType;
59     }
60     public void setContentType(String contentType) {
61         this.contentType = contentType;
62     }
63     
64 }

download.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6 <package name="download" namespace="/download" extends="struts-default">
 7   <action name="download" class="cn.hmy.download.downloadAction" method="download">
 8     <param name="inputPath">/images</param>
 9     <result name="success" type="stream">
10       <param name="contentType">application/octet-stream</param>
11       <param name="inputName">inputStream</param>
12       <param name="contentDisposition">
13         attachment;filename="${filename}"
14       </param>
15       <param name="bufferSize">4096</param>
16     </result>
17   </action>
18 </package>
19 </struts>

download.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3     <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <%@taglib uri="/struts-tags" prefix="s" %>
 8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
12 <title>文件下载</title>
13 </head>
14 <body>
15 <img src="<%=path %>/images/06.jpg"></img>
16 <a href="download/download?filename=06.jpg">点击此处下载文档</a>
17 </body>
18 </html>

在地址栏中请求

 

 

posted @ 2016-09-06 16:24  回青  阅读(210)  评论(0编辑  收藏  举报