本篇文章為大家展示了使用struts2如何實(shí)現(xiàn)文件下載功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
10年的宏偉網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷(xiāo)型網(wǎng)站的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整宏偉建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“宏偉網(wǎng)站設(shè)計(jì)”,“宏偉網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
1. 項(xiàng)目結(jié)構(gòu)

2. web.xml
<?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"> <!-- 設(shè)置struts 2過(guò)濾器 --> <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> <!-- 設(shè)置歡迎頁(yè)面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- session超時(shí)定義,單位為分鐘 --> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
3.DownloadAction.java
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,文件下載關(guān)鍵方法
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
<?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 應(yīng)用中的常量 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 配置上傳文件的最大容量,struts2默認(rèn)為2M。單位是1B, 1KB=1024B,1M=1024KB,1M=1024*1024B--> <constant name="struts.multipart.maxSize" value="1048576" /> <!-- 配置本應(yīng)用中的包,繼承 struts-default 包 --> <package name="FileDownload" namespace="/" extends="struts-default"> <action name="download" class="com.action.DownloadAction"> <!-- 設(shè)置文件路徑的參數(shù),傳到action類(lèi)文件中去 --> <!-- <param name="path">\download\a.jpg</param> --> <!-- 下載文件類(lèi)型定義,即定義為“stream” --> <result name="success" type="stream"> <!-- image/jpeg代表JPG圖片 --> <param name="contentType">image/jpeg</param> <!-- 下載文件處理方法 --> <param name="contentDisposition"> <!-- attachment表示附件方式,即下載時(shí)打開(kāi)保存對(duì)話窗,filename表示下載時(shí)顯示的保存時(shí)的文件名 --> <!-- 如果不寫(xiě)attachment;或者是寫(xiě)的是inline; 則表示內(nèi)聯(lián),即會(huì)在瀏覽器中嘗試打開(kāi)下載的文件,而不是下載--> attachment;filename="a.jpg" </param> <!-- 下載文件輸出流定義 --> <!-- 這里的inputName元素所對(duì)應(yīng)的value值downloadFile,在action中一定要有對(duì)應(yīng)的getDownloadFile()方法 --> <param name="inputName">downloadFile</param> <!-- 下載緩沖區(qū)的大小 --> <param name="bufferSize">1024</param> </result> </action> </package> </struts>
5.index.jsp
<%@ 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>首頁(yè)</title> </head> <body> <center> 歡迎來(lái)到首頁(yè),點(diǎn)下面鏈接去下載一個(gè)文件<br /> <a href="download.action?path=<%=" rel="external nofollow" ./download/a.jpg" %>">下載</a> </center> </body> </html>
上述內(nèi)容就是使用struts2如何實(shí)現(xiàn)文件下載功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文標(biāo)題:使用struts2如何實(shí)現(xiàn)文件下載功能
轉(zhuǎn)載注明:http://chinadenli.net/article32/jgjisc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、標(biāo)簽優(yōu)化、、搜索引擎優(yōu)化、企業(yè)建站、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)