国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Struts2實現文件下載功能代碼分享(文件名中文轉碼)

2019-11-26 14:15:28
字體:
來源:轉載
供稿:網友

struts2文件下載功能實現代碼如下所示:

Action文件

public class DownLoadAction extends ActionSupport { /** * */ private static final long serialVersionUID = 5879762231742395104L; private String fileName;//用戶請求的文件名 private String inputPath;//下載資源的路徑(在struts配置文件中設置) 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); //防止用戶請求不安全的資源 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); } /* * 獲取下載時文件默認的文件名 */ 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設置為stream --> <result name="download_success" type="stream"> <!-- MIME類型 --> <param name="contentType">application/octet-stream</param> <!-- inputName的值與action獲取輸入流資源的方法名相對應(action中定義getInputStream方法,并且返回類型為InputStream) --> <param name="inputName">inputStream</param> <!-- 設置帶附件的文件 動態獲取文件名(action中定義getDownloadFileName方法) --> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <!-- 設置緩沖大小 --> <param name="bufferSize">2048</param> </result> </action> 

以上所述是小編給大家介紹的Struts2實現文件下載功能代碼分享(文件名中文轉碼)的相關內容,希望對大家有所幫助!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陈巴尔虎旗| 南和县| 青州市| 台中县| 萨嘎县| 海口市| 华亭县| 龙岩市| 昭平县| 襄城县| 隆回县| 游戏| 彭阳县| 新营市| 镇平县| 保德县| 中方县| 开鲁县| 理塘县| 莱芜市| 鄂州市| 苗栗县| 镇江市| 蒲江县| 东明县| 怀安县| 常山县| 凌云县| 揭阳市| 本溪市| 遵义市| 化州市| 山东省| 榆社县| 山丹县| 晋中市| 黄梅县| 合山市| 增城市| 汨罗市| 扬州市|