Set the Chinese name (response. addHeader) for java file download ),

Source: Internet
Author: User

Set the Chinese name (response. addHeader) for java file download ),

1 protected void doPost (HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException {3 String browser = ""; 4 String fileName = "test .txt"; 6 try {7 browser = request. getHeader ("User-Agent"); 8 if (-1 <browser. indexOf ("MSIE 6.0") |-1 <browser. indexOf ("MSIE 7.0") {9 // IE6, IE7 browser 10 response. addHeader ("content-disposition", "attachment; filename =" 11 + new String (fileName. getBytes (), "ISO8859-1"); 12} else if (-1 <browser. indexOf ("MSIE 8.0") {13 // IE8 14 response. addHeader ("content-disposition", "attachment; filename =" 15 + URLEncoder. encode (fileName, "UTF-8"); 16} else if (-1 <browser. indexOf ("MSIE 9.0") {17 // IE918 response. addHeader ("content-disposition", "attachment; filename =" 19 + URLEncoder. encode (fileName, "UTF-8"); 20} else if (-1 <browser. indexOf ("Chrome") {21 // Google 22 response. addHeader ("content-disposition", 23 "attachment; filename x = UTF-8'' "+ URLEncoder. encode (fileName, "UTF-8"); 24} else if (-1 <browser. indexOf ("Safari") {25 // Apple 26 response. addHeader ("content-disposition", "attachment; filename =" 27 + new String (fileName. getBytes (), "ISO8859-1"); 28} else {29 // Firefox or another browser 30 response. addHeader ("content-disposition", 31 "attachment; filename x = UTF-8'' "+ URLEncoder. encode (fileName, "UTF-8"); 32} 33} catch (Exception e) {34 e. printStackTrace (); 35} 36 InputStream in = new FileInputStream ("D: \ test.txt"); // obtain the file input stream 37 int len = 0; 38 byte [] buffer = new byte [1024]; 39 OutputStream out = response. getOutputStream (); 40 while (len = in. read (buffer)> 0) {41 out. write (buffer, 0, len); // outputs the buffer data to the client browser 42} 43 in. close (); 44 out. flush (); 45 out. close (); 46}

Judge the various browsers, and then perform decoding and Encryption

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.