A brief discussion on the download function in JSP pages

Source: Internet
Author: User
Tags access
js| Download | page If just under a file, directly with a hyperlink can be done but the chain is not safe, who can access, with the program of course good, but the Web can not access the directory of the file download to the authenticated visitors.

I have previously written an example of taking pictures out of the database and pushing them to the page.
You simply change the data source from the database to a data source read from a file, and then

"Image/gif"
Instead of "Application/x-msdownload", you can output a file to a browser.


File t_file = new Java.io.File (sourcefilepathname);
Long L = t_file.length (); File length
InputStream in = new FileInputStream (t_file);
if (in!= null)
{
String fs = T_file.getname ();
Response.reset ();
Response.setcontenttype ("Application/x-msdownload"); //
String s = "attachment;" Filename= "+FS; //
Response.setheader ("Content-disposition", s); Above output file meta information
Response.setcontentlength ((int) l); Set input file length
Byte[] B = new byte[2048];
int len = 0;
while ({Len=in.read (b)) >0);
{
Response.getoutputstream (). write (B,0,len); Output to browser \ r
}
In.close (); Close file input stream \ r
}



Related Article

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.