Some of the main packages and classes
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import java.io.UnsupportedEncodingException;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.net.URLDecoder;
Import Java.net.URLEncoder;
/**
* Download File
* @return
* @throws unsupportedencodingexception
*/
@Action (value= "Downloadattachfile")
Public String Downloadattachfile () {
Attachments from the front desk name: Format is, "2006# system test. docx format"
String downloadname =wpyid+ "#" +ATTACHNAME;
Data passed in
System.out.println (Downloadname);
try {
OutputStream OS = This.response.getOutputStream ();
OutputStream OS = This.getresponse (). Getoutputstream ();
if (Util.isie (this.request))//Determine if the client is IE
{
Coding
Downloadname = Urlencoder.encode (Downloadname, "UTF-8");
}
Else
{
Downloadname = new String (downloadname.getbytes ("UTF-8"), "iso-8859-1");
}
Set the header for the browser to pop up the download dialog box
This.response.setContentType ("Application/x-download");
This.response.addHeader ("Content-disposition", "attachment;filename=\" "+ downloadname +" \ ");
This.response.flushBuffer ();
The foreground data is packaged (header data is encoded) and needs to be decoded.
String downloadName1 =urldecoder.decode (downloadname, "UTF-8");
System.out.println (DOWNLOADNAME1);
Decoded result output
String Bgfile=realpath + file.separator + downloadName1;
FileInputStream fis = new FileInputStream (bgfile);
Util.copystrem (FIS, OS);
Fis.close ();
Os.close ();
System.out.println ("========== success!! ===========");
} catch (IOException e) {
E.printstacktrace ();//The location and cause of error in the program when printing exception information in the command line
System.out.println ("========== made a mistake!! ===========");
}
return NONE;
}
How to download attachments in JSP action