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 ()
}