日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

使用struts2的 下载

發布時間:2025/3/12 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用struts2的 下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

jsp:

<a href="testDownloadFiles!xiazai.action?fileName=aaa.rar">不帶中文名的文件</a><br> <a href="testDownloadFiles!xiazai.action?fileName=<%=java.net.URLEncoder.encode("宜春院.rar","utf-8").toString().replace("%","_") %>">下載帶中文的文件</a>
配置文件struts.xml

<package name="sk-default" extends="struts-default,json-default"> <action name="testDownloadFiles" class="com.TestDownloadFiles"> <result name = "input" type="stream"><param name="inputName">downloadFile</param><param name="bufferSize">1024</param><param name="contentDisposition">attachment;filename="${fileName}"</param></result></action></package>
java:

private String fileName;public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}//下載public String xiazai(){System.out.println("執行下載");return "input";}//配置文件中對應的屬性<param name="inputName">downloadFile</param> public InputStream getDownloadFile() throws UnsupportedEncodingException, FileNotFoundException{fileName = fileName.replace("_","%");fileName = java.net.URLDecoder.decode(fileName,"utf-8");System.out.println("轉碼前:"+fileName);response.setContentType("application/x-msdownload");FileInputStream fileInputStream = new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("")+"\\"+fileName));//文件物理盤位置fileName = new String(fileName.getBytes("GBK"),"iso-8859-1");System.out.println("下載文件名(轉碼后):"+fileName);//response.setHeader("Content-disposition","attachment;filename="+fileName);return fileInputStream;}}

總結

以上是生活随笔為你收集整理的使用struts2的 下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。