Instance:
String poorname= datamap.get ("NAME"). ToString ();
String villagename = Datamap.get ("ADDRESS"). ToString ();
String filename=villagename+ "-" +poorname+ ". Doc";
Response.setcontenttype ("Application/doc");
Final String useragent = Request.getheader ("user-agent");
if (Stringutils.contains (useragent, "MSIE")) {//ie browser
filename = urlencoder.encode (filename, "UTF-8");
}else if (Stringutils.contains (useragent, "Mozilla")) {//google, Firefox browser
filename = new String (Filename.getbytes (), "iso8859-1");
}else{
filename = urlencoder.encode (filename, "UTF-8");//Other browsers
}
Response.AddHeader ("Content-disposition", "Attachment;filename="
+filename);/set up here to let the browser pop up the download prompt instead of opening it directly in the browser
Writer out = Response.getwriter ();
You can also extract the method directly for later use:
Setfiledownloadheader function is mainly based on the current user's browser, the name of the file of different encoding settings, so as to solve the different browser file name Chinese garbled problem
public static void Setfiledownloadheader (HttpServletRequest request, httpservletresponse response, String fileName) {
Final String useragent = Request.getheader ("user-agent");
try {
String finalfilename = null;
if (Stringutils.contains (useragent, "MSIE")) {//ie browser
Finalfilename = Urlencoder.encode (FileName, "UTF8");
}else if (Stringutils.contains (useragent, "Mozilla")) {//google, Firefox browser
Finalfilename = new String (Filename.getbytes (), "iso8859-1");
}else{
Finalfilename = Urlencoder.encode (FileName, "UTF8");//Other browsers
}
Response.setheader ("Content-disposition", "attachment; Filename=\ "" + finalfilename + "\" ");//Set up here to let the browser pop up the download box instead of opening it directly in the browser
} catch (Unsupportedencodingexception e) {
}
}
Java file download and export filenames garbled browser compatibility issues