Javaweb use struts to implement file download change file name _java

Source: Internet
Author: User

First look at the effect chart:

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, Action Form form, HttpServletRequest request, httpservletresponse response) throws Exception {//Get the name of the resource file String resname = Reque

St.getparameter ("Resname"); Use struts to download the resource return to new Downloadaction.resourcestreaminfo ("Audio/x-mpeg", this. Getservlet (). Getservletcontext (),
"/resource/music/" + resname); @Override public Actionforward Execute (actionmapping mapping, actionform form, httpservletrequest request, HttpServlet Response Response) throws Exception {//Send to BrowserSends a message header, causes the file to open as an attachment, and finds the file name corresponding to the resource name sent to the browser String resname = Request.getparameter ("Resname");
FileUtils fu = new FileUtils (This.getservlet (). Getservletcontext ());
map<string,string> Musmap = Fu.getmusmap ();

String name = Musmap.get (resname); The file name is also encoded.
In Firefox or there are garbled, do not understand why, hope advice.
Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode (name, "UTF-8"));
return Super.execute (mapping, form, request, response);


 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.