Several different methods of JSP File Download

Source: Internet
Author: User

1. the most direct and simple way to download JSP files is to put the file address directly into a link on the html page. The disadvantage is that the file path on the server is exposed, and the file download cannot be controlled, such as permissions ). This will not be used as an example.

2. on the server side, convert the file into an output stream, write it to response, and take the file to the browser. The browser will prompt you whether you want to save the file locally. JSP File Download example)

 
 
  1. <%
  2. Response. setContentType (fileminitype );
  3. Response. setHeader ("Location", filename );
  4. Response. setHeader ("Cache-Control ","Max-age= "+ CacheTime );
  5. Response. setHeader ("Content-Disposition", "attachment;Filename= "+ Filename); // filename should be encoded (UTF-8)
  6. Response. setContentLength (filelength );
  7. OutputStreamOutputStream=Response. GetOutputStream ();
  8. InputStreamInputStream=NewFileInputStream (filepath );
  9. Byte []Buffer=NewByte [2, 1024];
  10. IntI=-1;
  11. While ((I=InputStream. Read (buffer ))! =-1 ){
  12. OutputStream. write (buffer, 0, I );
  13. }
  14. OutputStream. flush ();
  15. OutputStream. close ();
  16. InputStream. close ();
  17.  OutputStream=Null;
  18.  
  19. %> 

3. Since it is a JSP file download, there is also a JSP file download method that uses Applet to download files. However, the customer must first trust your Applet to accept the data streams sent by the servlet and write them to the local machine.

Servlet example

 
 
  1. Public void service (HttpServletRequest req, HttpServletResponse res)
  2. Throws ServletException, IOException {
  3. Res. setContentType ("text/plain ");
  4. OutputStreamOutputStream=Null;
  5. Try {
  6. OutputStream=Res. GetOutputStream ();
  7. PopFile (srcFile, outputStream); // write the file path srcFile to outputStream.
  8. } Catch (IOException e ){
  9. E. printStackTrace ();
  10. }
  11. }

JApplet sample

URLConnection con;

  1. Analysis on CGI supported by jsp http Server
  2. Use JSP pages to generate PDF reports
  3. Step for customizing JSP labels
  4. Detailed test of JSP containers
  5. Describes the following features of jsp http Server

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.