JavaWeb應用struts完成文件下載時轉變文件稱號。本站提示廣大學習愛好者:(JavaWeb應用struts完成文件下載時轉變文件稱號)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaWeb應用struts完成文件下載時轉變文件稱號正文
先看看後果圖:
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 { //向閱讀器發送新聞頭,使文件以附件的方法翻開,並找到資本名對應的文件稱號發送給閱讀器 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); } }
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。