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.