File Download Key code

Source: Internet
Author: User

String filename = request.getparameter ("filename");
Get the output stream that you want to output from the client
OutputStream outputstream = Response.getoutputstream ();
An array of bytes for the output file, sending 600 bytes to the output stream at a time
byte b[] = new byte[600];
The file to download
File Fileload=new file ("c://exportexecl/" +filename+ "");
The client side uses the dialog box to save the file
Response.AddHeader ("Content-disposition", "Attachment;filename="
+ java.net.URLEncoder.encode (filename, "UTF-8"));
MIME type of notification service file
Response.setcontenttype (Getservletcontext (). GetMimeType (filename));
Notice the length of the customer service file
Long filelength = Fileload.length ();
String length = string.valueof (filelength);
Response.setheader ("content_length", length);
Read the file and send it to the customer service side for download
FileInputStream InputStream = new FileInputStream (fileload);
int n = 0;
while ((N=inputstream.read (b))!=-1) {
Outputstream.write (B,0,n);
}
Clear Cache
if (fileload.exists ()) {
Fileload.delete ();
SYSTEM.OUT.PRINTLN ("The server cache has been cleared! ");
}

File Download Key code

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.