JavaWeb利用struts實現(xiàn)文件下載時改變文件名稱
更新時間:2016年06月29日 10:38:19 投稿:lijiao
這篇文章主要為大家詳細介紹了JavaWeb利用struts實現(xià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 { //獲取資源文件的名稱 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 { //向瀏覽器發(fā)送消息頭,使文件以附件的方式打開,并找到資源名對應的文件名稱發(fā)送給瀏覽器 String resname = request.getParameter("resname"); FileUtils fu = new FileUtils(this.getServlet().getServletContext()); Map<String,String> musmap = fu.getMusmap(); String name = musmap.get(resname); //同時對文件名進行編碼。在FireFox下還是有亂碼,不明白原因,望指教。 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8")); return super.execute(mapping, form, request, response); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
spring AOP的Around增強實現(xiàn)方法分析
這篇文章主要介紹了spring AOP的Around增強實現(xiàn)方法,結(jié)合實例形式分析了spring面向切面AOP的Around增強具體步驟與相關操作方法,需要的朋友可以參考下2020-01-01IDEA無法打開Marketplace的三種解決方案(推薦)
這篇文章主要介紹了IDEA無法打開Marketplace的三種解決方案(推薦),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11詳解SpringBoot項目的創(chuàng)建與單元測試
這篇文章主要介紹了詳解SpringBoot項目的創(chuàng)建與單元測試,幫助大家更好的理解和學習使用SpringBoot,感興趣的朋友可以了解下2021-03-03