Struts2實(shí)現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)
更新時(shí)間:2016年06月02日 14:26:50 作者:laozhan1028
這篇文章主要介紹了Struts2實(shí)現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)的相關(guān)資料,需要的朋友可以參考下
struts2文件下載功能實(shí)現(xiàn)代碼如下所示:
Action文件
public class DownLoadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 5879762231742395104L;
private String fileName;//用戶請(qǐng)求的文件名
private String inputPath;//下載資源的路徑(在struts配置文件中設(shè)置)
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public String getInputPath() {
return inputPath;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
public String downloadFile() throws Exception {
ServletContext context = ServletActionContext.getServletContext();
String downloadDir = context.getRealPath("/upload");
String downloadFile = context.getRealPath(inputPath);
//防止用戶請(qǐng)求不安全的資源
if(!downloadFile.startsWith(downloadDir)) {
return null;
}
return "download_success";
}
/*
* 獲取輸入流資源
*/
public InputStream getInputStream() throws Exception {
String path = inputPath + File.separatorChar + new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
return ServletActionContext.getServletContext().getResourceAsStream(path);
}
/*
* 獲取下載時(shí)文件默認(rèn)的文件名
*/
public String getDownloadFileName() {
String downloadFileName = fileName;
try {
downloadFileName = URLEncoder.encode(downloadFileName, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.getMessage();
e.printStackTrace();
}
return downloadFileName;
}
}
jsp文件:
<li> <a href="<%=path%>/download_DownLoadAction_downloadFile.action?fileName=dwr.jar">dwr.jar文件下在</a> </li> <li> <a href="<%=path%>/download_DownLoadAction_downloadFile.action?fileName=Struts2教程.pdf">Struts2教程.pdf文件下在</a> </li>
struts.xml:
<action name="download_*_*" class="com.wang.action.{1}" method="{2}">
<param name="inputPath">/upload</param>
<!-- result type設(shè)置為stream -->
<result name="download_success" type="stream">
<!-- MIME類型 -->
<param name="contentType">application/octet-stream</param>
<!-- inputName的值與action獲取輸入流資源的方法名相對(duì)應(yīng)(action中定義getInputStream方法,并且返回類型為InputStream) -->
<param name="inputName">inputStream</param>
<!-- 設(shè)置帶附件的文件 動(dòng)態(tài)獲取文件名(action中定義getDownloadFileName方法) -->
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<!-- 設(shè)置緩沖大小 -->
<param name="bufferSize">2048</param>
</result>
</action>
以上所述是小編給大家介紹的Struts2實(shí)現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)的相關(guān)內(nèi)容,希望對(duì)大家有所幫助!
您可能感興趣的文章:
- struts2實(shí)現(xiàn)簡(jiǎn)單文件下載功能
- JSP開發(fā)之Struts2實(shí)現(xiàn)下載功能的實(shí)例
- struts2實(shí)現(xiàn)文件下載功能
- java中Struts2 的文件上傳和下載示例
- java中struts2實(shí)現(xiàn)簡(jiǎn)單的文件上傳與下載
- java中struts2實(shí)現(xiàn)文件上傳下載功能
- JavaWeb中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2實(shí)現(xiàn)文件下載功能
相關(guān)文章
Java輕松掌握面向?qū)ο蟮娜筇匦苑庋b與繼承和多態(tài)
本文主要講述的是面向?qū)ο蟮娜筇匦裕悍庋b,繼承,多態(tài),內(nèi)容含括從封裝到繼承再到多態(tài)的所有重點(diǎn)內(nèi)容以及使用細(xì)節(jié)和注意事項(xiàng),內(nèi)容有點(diǎn)長(zhǎng),請(qǐng)大家耐心看完2022-05-05
java實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
淺談java中為什么重寫equals后需要重寫hashCode
今天帶各位學(xué)習(xí)一下java中為什么重寫equals后需要重寫hashCode,文中有非常詳細(xì)的圖文介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05
IDEA使用SequenceDiagram插件繪制時(shí)序圖的方法
這篇文章主要介紹了IDEA使用SequenceDiagram插件繪制時(shí)序圖的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

