How to download attachments in JSP action

Source: Internet
Author: User

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

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.