[Servlet] writes the file to the Respond output stream for users to download, and the respond output stream

Source: Internet
Author: User

[Servlet] writes the file to the Respond output stream for users to download, and the respond output stream

This article distinguishes the content originally printed to the console by writing Java to the Respond output stream for users to download (click to open the link, directly print it to a text file txt and download it to the user.

In fact, [Struts2] Chinese File Download and download permission control "(click to open the link) does not use three frameworks, which are purely implemented using servlets, how to download the file resources on the Tomcat server.

For example, the javaeedirectory structure is shown in the figure chrysanthemum.jpg at the root of the servletdownloadtestproject on the Tomcat server.


However, if you enter http: // localhost: 8080/ServletDownloadTest/download. java to access the Servlet "download", a download prompt box appears, for example:


After downloading the image, you can open it instead of directly displaying the image tag on the webpage like the img tag.

The display here is very simple. First of all, because the comments of Servlet3.0 are used, there is nothing in web. xml,

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"></web-app>
The key is the code in download. java. After obtaining the real path where the server stores the file, write the file to the buffer first, and then write the buffer to the respond output stream for users to download.

Import java. io. *; import java.net. *; import javax. servlet. *; import javax. servlet. http. *; import javax. servlet. annotation. *; @ WebServlet (name = "download", urlPatterns = {"/download"}) public class download extends HttpServlet {private static final long serialVersionUID = 1L; protected void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String file Path = this. getServletContext (). getRealPath ("/"); // obtain the actual path String downloadFilename = ".jpg with no question in Chinese"; // The default file name downloadFilename = URLEncoder is displayed in the download box. encode (downloadFilename, "UTF-8"); // encode the default downloaded file name. The unencoded result is that the File name is garbled during client download. file = new File (filePath + File. separator + "Chrysanthemum.jpg"); if (file. exists () {// specify the size of the file to be downloaded response. setContentLength (int) file. length (); response. setHeader ("Content-Disposition", "attachment; filename =" + downloadFilename); // set the default file name response displayed in the download box. setContentType ("application/octet-stream "); // specify that the returned object of response is a file stream // read the file to response // here, you first need to read the file content to the buffer zone // then write the buffer content to response output stream for users to download FileInputStream fileInputStream = new FileInputStream (file ); bufferedInputStream bufferedInputStream = new BufferedInputStream (fileInputStream); byte [] B = new byte [bufferedInputStream. available ()]; bufferedInputStream. read (B); OutputStream outputStream = response. getOutputStream (); outputStream. write (B); // walk bufferedInputStream. close (); outputStream. flush (); outputStream. close ();}}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.