How to display the Chinese name of response. setheader in File Download

Source: Internet
Author: User

Def download = {
Println Params. ID = NULL? "The file does not exist": Params. ID
Def documentinstance = Document. Get (Params. ID)
Def filename = documentinstance.doc umname
Println filename
String temp = filename;

If (request. getheader ("User-Agent"). tolowercase (). indexof ("Firefox")> 0 ){
Filename = new string (filename. getbytes ("UTF-8"), "ISO8859-1"); // Firefox
} Else if (request. getheader ("User-Agent"). touppercase (). indexof ("MSIE")> 0 ){
Filename = urlencoder. encode (filename, "UTF-8"); // IE browser
}
Response. Reset (); // if there is a line break, there is no problem with the text file, but for other cells
// Format. For example, some line breaks/0x0d and 0x0a will appear in files downloaded from AutoCAD, Word, Excel, and other files. This may cause files in some formats to fail to be opened, some of them can be opened normally. At the same time, the response. Reset () method can also clear the buffer, preventing empty rows in the page from being output to the download content.
Response. setcontenttype ("application/octet-stream ");
Response. setheader ("content-disposition", "attachment; filename = \" "+ filename + "\"");
Response. setheader ("connection", "close ");


Def webrootdir = servletcontext. getrealpath ("/")
Def savename = documentinstance. savename
Def filepath = new file (webrootdir, "/upload/$ {savename }")
Println filepath
Def out = response. outputstream
Def inputstream = new fileinputstream (filepath)
Byte [] buffer = new byte [1024*1024]
Int I =-1
While (I = inputstream. Read (buffer ))! =-1 ){
Out. Write (buffer, 0, I)
}
Out. Flush ()
Out. Close ()
Inputstream. Close ()
}

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.