Several methods for implementing the file download function using JSP

Source: Internet
Author: User

 

1. the most direct and simplest way 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 other control (such as permissions) on file download is not available ). 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. (Example)

 

 

<% Response. setcontenttype (fileminitype); response. setheader ("location", filename); response. setheader ("cache-control", "Max-age =" + cachetime); // the filename should be encoded (UTF-8) response. setheader ("content-disposition", "attachment; filename =" + filename); response. setcontentlength (filelength); outputstream = response. getoutputstream (); inputstream = new fileinputstream (filepath); B Yte [] buffer = new byte [1024]; int I =-1; while (I = inputstream. Read (buffer ))! =-1) {outputstream. write (buffer, 0, I);} outputstream. flush (); outputstream. close (); inputstream. close (); outputstream = NULL; %>

 

3. Since JSP is used, another method is to use 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

 

 

Public void Service (httpservletrequest req, httpservletresponse res) throws servletexception, ioexception {res. setcontenttype ("text/plain"); outputstream = NULL; try {outputstream = res. getoutputstream (); // write the file whose path is srcfile to popfile (srcfile, outputstream) in outputstream;} catch (ioexception e) {e. printstacktrace ();}}

 

Japplet sample

 

 

Urlconnection con; try {// URL is the URL of the called servlet, such *. do con = URL. openconnection (); con. setusecaches (false); con. setdoinput (true); con. setdooutput (true); con. setrequestproperty ("Content-Type", "application/octet-stream"); inputstream in = con. getinputstream (); progressmonitorinputstream pminputstream = new progressmonitorinputstream (PANE, "downloading file content from the server", in); progressmonitor pmonitor = pminputstream. getprogressmonitor (); pmonitor. setmillistodecidetopopup (3); pmonitor. setmillistopopup (3); // localfilepath local path, localstr file folder, filename local file name string localfilepath = localstr + filename; // The savefilsavefilee method writes the input stream pminputstream to the file localfilepath if (savefilsavefilee (localfilepath, pminputstream) {openlocalfile (localfilepath );}

 

 

4. By the way, the japplet Upload File Code is also pasted.

 

Japplet sample

 

 

Urlconnection con; try {con = URL. openconnection (); // The URL is the URL of the called servlet, for example *. do con. setusecaches (false); con. setdoinput (true); con. setdooutput (true); con. setrequestproperty ("Content-Type", "application/octet-stream"); outputstream out = con. getoutputstream (); // localfilepath local path, localstr file folder, filename local file name string localfilepath = localstr + filename; // The getoutputstream file writes the localfilepath file to the output stream out to get Outputstream (localfilepath, out); inputstream in = con. getinputstream (); Return true;} catch (ioexception e) {system. Out. println ("File Upload error! "); E. printstacktrace ();}

 

 

Sample servlet code

 

 

Public void Service (httpservletrequest req, httpservletresponse res) throws servletexception, ioexception {res. setcontenttype ("text/plain"); inputstream = NULL; try {inputstream = res. getinputstream (); // Save the input stream inputstream to the writefile (srcfile, inputstream) in the file path srcfile;} catch (ioexception e) {e. printstacktrace () ;}// end Service

 

 

Summary: The file is transmitted in the form of a stream and stored in the form of a file on the hard disk. All we need to do is send streams and read streams through httpservletrequest and httpservletresponse, or response and request. And convert a file into a stream or convert it into a file.

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.