Struts2實(shí)現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎ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開(kāi)發(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)文章
nacos中的配置使用@Value注解獲取不到值的原因及解決方案
這篇文章主要介紹了nacos中的配置使用@Value注解獲取不到值的原因分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
springboot集成nacos無(wú)法動(dòng)態(tài)獲取nacos配置的問(wèn)題
這篇文章主要介紹了springboot集成nacos無(wú)法動(dòng)態(tài)獲取nacos配置的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
詳解java CountDownLatch和CyclicBarrier在內(nèi)部實(shí)現(xiàn)和場(chǎng)景上的區(qū)別
這篇文章主要介紹了詳解java CountDownLatch和CyclicBarrier在內(nèi)部實(shí)現(xiàn)和場(chǎng)景上的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
SpringSecurityOAuth2 如何自定義token信息
這篇文章主要介紹了SpringSecurityOAuth2 自定義token信息的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
JavaWeb實(shí)現(xiàn)注冊(cè)用戶名檢測(cè)
這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)注冊(cè)用戶名檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
SpringCloud-Hystrix實(shí)現(xiàn)原理總結(jié)
通過(guò)hystrix可以解決雪崩效應(yīng)問(wèn)題,它提供了資源隔離、降級(jí)機(jī)制、融斷、緩存等功能。接下來(lái)通過(guò)本文給大家分享SpringCloud-Hystrix實(shí)現(xiàn)原理,感興趣的朋友一起看看吧2021-05-05

