A good memory is better than a bad pen. 13-File Download with HttpServletResponse

Source: Internet
Author: User


in JS can also achieve file download, but in the JS link, very easy to be hotlinking, and easy to cause attacks. A link to the download of the file is placed on the Java side, allowing for more validation of the user's request, preventing part of the hotlinking and attacks.

1 , implementation of download function using HttpServletResponse

1. get the absolute path to the file you want to download

2. get the file name to download

3. Set the Content-disposition response header to control the browser to download the form of open file

4. get the file input stream to download

5. Create a data buffer

6. get the OutputStream stream from the response object

7. writing the FileInputStream stream to buffer buffers

8. using OutputStream to output buffer data to the client browser

2 , using HttpServletResponse to implement download considerations

File Download Note: It is recommended to use OutputStream stream when writing file download function , avoid using printwriter stream, because OutputStream stream is byte stream, can handle any kind of data, The PrintWriter stream, which is a character stream, can handle only characters, which can result in data loss if the byte data is processed using a character stream.

when downloading Chinese files, it is important to note that the Chinese file name is encoded using the Urlencoder.encode method (Urlencoder.encode (filename, "character encoding")), otherwise the file name will be garbled.

Response.setheader ("Content-disposition", "attachment;filename=" +urlencoder.encode (filename, "UTF-8"));

3 , HttpServletResponse Implementation of the downloaded source code

Package com.servlet;

Import Java.awt.Color;

Import Java.awt.Font;

Import Java.awt.Graphics2D;

Import Java.awt.image.BufferedImage;

Import Java.io.FileInputStream;

Import java.io.IOException;

Import Java.io.InputStream;

Import Java.io.OutputStream;

Import Java.util.Random;

Import Javax.imageio.ImageIO;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Importjavax.servlet.http.HttpServletRequest;

Importjavax.servlet.http.HttpServletResponse;

/**

 * Simple File download via HttpServletResponse

*

* @author Fan Fangming

*/

public class Responsedownload Extendshttpservlet {

Publicvoid doget (httpservletrequest request, httpservletresponse response)

Throwsservletexception, IOException {

//1. gets the absolute path of the file to download

Stringrealpath = This.getservletcontext (). Getrealpath ("/files/fxjh.jpg");

//2. get the file name to download

Stringfilename = realpath.substring (realpath.lastindexof ("\ \") + 1);

//3. set the Content-disposition response header to control the browser download form to open the file

Response.setheader ("Content-disposition", "Attachment;filename="

+filename);

//4. get the file input stream to download

Inputstreamin = new FileInputStream (Realpath);

Intlen = 0;

//5. creating a data buffer

Byte[]buffer = new byte[1024];

//6. Getting OutputStream streams from response objects

Outputstreamout = Response.getoutputstream ();

//7. Writes the FileInputStream stream to buffer buffers

while (len = in.read (buffer)) > 0) {

//8. using OutputStream to output buffer data to the client browser

Out.write (buffer,0, Len);

}

In.close ();

}

Publicvoid DoPost (httpservletrequest request, httpservletresponse response)

Throwsservletexception, IOException {

Doget (Request,response);

}

}

A good memory is better than a bad pen. 13-File Download with HttpServletResponse

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.