JavaWeb利用struts實(shí)現(xiàn)文件下載時(shí)改變文件名稱(chēng)
更新時(shí)間:2016年06月29日 10:38:19 投稿:lijiao
這篇文章主要為大家詳細(xì)介紹了JavaWeb利用struts實(shí)現(xiàn)文件下載時(shí)改變文件名稱(chēng)的相關(guān)資料,需要的朋友可以參考下
先看看效果圖:
package com.fenghuo.struts.download; import java.net.URLEncoder; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DownloadAction; import com.fenghuo.music.util.FileUtils; public class DownLoad extends DownloadAction { @Override protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //獲取資源文件的名稱(chēng) String resname = request.getParameter("resname"); //利用struts下載資源 return new DownloadAction.ResourceStreamInfo("audio/x-mpeg", this .getServlet().getServletContext(), "/resource/music/" + resname); } @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //向?yàn)g覽器發(fā)送消息頭,使文件以附件的方式打開(kāi),并找到資源名對(duì)應(yīng)的文件名稱(chēng)發(fā)送給瀏覽器 String resname = request.getParameter("resname"); FileUtils fu = new FileUtils(this.getServlet().getServletContext()); Map<String,String> musmap = fu.getMusmap(); String name = musmap.get(resname); //同時(shí)對(duì)文件名進(jìn)行編碼。在FireFox下還是有亂碼,不明白原因,望指教。 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8")); return super.execute(mapping, form, request, response); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring AOP的Around增強(qiáng)實(shí)現(xiàn)方法分析
這篇文章主要介紹了spring AOP的Around增強(qiáng)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了spring面向切面AOP的Around增強(qiáng)具體步驟與相關(guān)操作方法,需要的朋友可以參考下2020-01-01Jenkins使用Gradle編譯Android項(xiàng)目詳解
這篇文章主要介紹了Jenkins使用Gradle編譯Android項(xiàng)目詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07IDEA無(wú)法打開(kāi)Marketplace的三種解決方案(推薦)
這篇文章主要介紹了IDEA無(wú)法打開(kāi)Marketplace的三種解決方案(推薦),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11mybatis-plus樂(lè)觀鎖實(shí)現(xiàn)方式詳解
這篇文章主要介紹了mybatis-plus樂(lè)觀鎖實(shí)現(xiàn)方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01詳解SpringBoot項(xiàng)目的創(chuàng)建與單元測(cè)試
這篇文章主要介紹了詳解SpringBoot項(xiàng)目的創(chuàng)建與單元測(cè)試,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下2021-03-03解決Eclipse中java文件的圖標(biāo)變成空心J的問(wèn)題
這篇文章主要介紹了解決Eclipse中java文件的圖標(biāo)變成空心J的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01