Java solves the problem of garbled file names and java garbled characters when downloading files from IE browsers.

Source: Internet
Author: User

Java solves the problem of garbled file names and java garbled characters when downloading files from IE browsers.

/*** Differentiate ie from other browsers for garbled download files * @ param request * @ param fileName * @ return */public String getFileName (HttpServletRequest req, String fileName) {String userAgent = req. getHeader ("user-agent"); userAgent = null? "": UserAgent. toLowerCase (); String name = fileName; try {
// For IE or IE-based browsers: if (userAgent. contains ("msie") | userAgent. contains ("trident") {name = URLEncoder. encode (name, "UTF-8");} else {name = new String (name. getBytes (), "iso-8859-1");} catch (Exception e) {throw new SysException (ERRORConstants. COMMON_SYSTEM_ERROR, e);} return name ;}

In many examples on the internet, msie and like Gecko are used to distinguish between them. ie11 is used to remove msie;

The msie of like Gecko and ie11 was not removed and marked with a trident.

The conclusion is that like Gecko is not used for differentiation.

Public void exportExcel (HttpServletRequest req, HttpServletResponse response) {try {response. setContentType ("application/octet-stream"); String excelName = "file name"; response. addHeader ("Content-Disposition", "attachment; filename =" + getFileName (req, excelName) + ". xls "); OutputStream out = response. getOutputStream (); testService. export (out);} catch (Exception e ){//}}

 

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.