本文實例為大家分享了struts2下實現文件下載功能實例,供大家參考,具體內容如下
下面以實現一個圖片下載功能為例:
1. 項目結構
2. web.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
|
<?xml version= "1.0" encoding= "utf-8" ?> <web-app version= "3.0" xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http: //java.sun.com/xml/ns/javaee http: //java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 設置struts 2 過濾器 --> <filter> <filter-name>struts 2 </filter-name> <filter- class >org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter- class > </filter> <filter-mapping> <filter-name>struts 2 </filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 設置歡迎頁面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- session超時定義,單位為分鐘 --> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app> |
3.downloadaction.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
|
package com.action; import java.io.inputstream; import org.apache.struts2.servletactioncontext; import com.opensymphony.xwork2.actionsupport; public class downloadaction extends actionsupport{ private static final long serialversionuid = 1l; //文件路徑 private string path; //path屬性的getter方法 public string getpath(){ return path; } //path屬性的setter方法 public void setpath(string path){ this .path = path; } //返回inputstream,文件下載關鍵方法 public java.io.inputstream getdownloadfile() throws exception{ inputstream in = servletactioncontext.getservletcontext().getresourceasstream(getpath()); return in; } public string execute() throws exception{ return success; } } |
4.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
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd" > <struts> <!-- 配置 struts 2 應用中的常量 --> <constant name= "struts.i18n.encoding" value= "utf-8" /> <!-- 配置上傳文件的最大容量,struts2默認為2m。單位是1b, 1kb=1024b,1m=1024kb,1m= 1024 *1024b--> <constant name= "struts.multipart.maxsize" value= "1048576" /> <!-- 配置本應用中的包,繼承 struts- default 包 --> < package name= "filedownload" namespace= "/" extends = "struts-default" > <action name= "download" class = "com.action.downloadaction" > <!-- 設置文件路徑的參數,傳到action類文件中去 --> <!-- <param name= "path" >\download\a.jpg</param> --> <!-- 下載文件類型定義,即定義為“stream” --> <result name= "success" type= "stream" > <!-- image/jpeg代表jpg圖片 --> <param name= "contenttype" >image/jpeg</param> <!-- 下載文件處理方法 --> <param name= "contentdisposition" > <!-- attachment表示附件方式,即下載時打開保存對話窗,filename表示下載時顯示的保存時的文件名 --> <!-- 如果不寫attachment;或者是寫的是inline; 則表示內聯,即會在瀏覽器中嘗試打開下載的文件,而不是下載--> attachment;filename= "a.jpg" </param> <!-- 下載文件輸出流定義 --> <!-- 這里的inputname元素所對應的value值downloadfile,在action中一定要有對應的getdownloadfile()方法 --> <param name= "inputname" >downloadfile</param> <!-- 下載緩沖區的大小 --> <param name= "buffersize" > 1024 </param> </result> </action> </ package > </struts> |
5.index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+ "://" +request.getservername()+ ":" +request.getserverport()+path+ "/" ; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <base href= "<%=basepath%>" rel= "external nofollow" > <title>首頁</title> </head> <body> <center> 歡迎來到首頁,點下面鏈接去下載一個文件<br /> <a href= "download.action?path=<%=" rel= "external nofollow" ./download/a.jpg " %>" >下載</a> </center> </body> </html> |
6.文件路徑
項目中要提前建立好download目錄,和里面要存在有a.jpg文件,否則,下載會失敗。
7.功能入口
項目發布到服務器后,用瀏覽器訪問項目中的index.jsp ,點擊下載鏈接,就可以彈出“下載”對話框。
原文鏈接:https://blog.csdn.net/wangcunhuazi/article/details/41171707