Four Methods for JSP file download and four methods for jsp File Download

Source: Internet
Author: User

Four Methods for JSP file download and four methods for jsp File Download

For websites, websites often need to provide resources or materials for download. When it comes to download, the most primitive method is to provide the download URL on the webpage. There are several other file download methods described today. For which method is better, it depends on your own needs.

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 use response to bring the file to the browser. The browser will prompt you whether you want to save the file locally. The example is as follows:

<% 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 outputStream = response. getOutputStream (); InputStream inputStream = new FileInputStream (filepath); byte [] bu Ffer = 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 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 getOutputStre in the output stream out. Am (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 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.

The above is the JSP file download function method. I hope there is a method that will suit you and help you solve the implementation problem of the JSP file download function.

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.