Servlet File Download and servlet File Upload
Servlet file downloads are similar to file uploads.
When uploading a file, the key point is the formEnctypeSet the property valueMultipart/form-dataTo tell the server to send binary data to the server;
When downloading an object, the response isResponse. setContentType ("application/pdf ")Method, and specify the file name in the response header.Response. addHeader ("Content-Disposition", "attachment; filename = \" "+ filename + "\"")Tells the browser how to parse the stream file.
Specific Code
package com.filedownload;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class FileDownload */@WebServlet("/filedownload")public class FileDownload extends HttpServlet {private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FileDownload() { super(); // TODO Auto-generated constructor stub }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String dataDirectory = request. getServletContext().getRealPath("/WEB-INF/");String filename = "Java Persistence with MyBatis 3.pdf"; File file = new File(dataDirectory, filename); if (file.exists()) { response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); byte[] buffer = new byte[1024]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); OutputStream os = response.getOutputStream(); int i = bis.read(buffer); while (i != -1) { os.write(buffer, 0, i); i = bis.read(buffer); } } catch (IOException ex) { System.out.println (ex.toString()); } finally { if (bis != null) { bis.close(); } if (fis != null) { fis.close(); } } }}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
The key part of the code is file acquisition and response settings in the doGet method.
String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/");String filename = "Java Persistence with MyBatis 3.pdf";File file = new File(dataDirectory, filename);
These three lines of code get the file to be downloaded
Request. getServletContext (). getRealPath ("/WEB-INF/"); gets the real path of the WEB-INF directory, filename specifies the name of the file to be downloaded
If the file to be downloaded is not in the tomcat container, you can use the absolute path to point to the actual path of the file.
For example, File file = new File ("d: \ test.pdf ");
response.setContentType("application/pdf");response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
Response. setContentType ("application/pdf") sets the mime type of the file, telling the browser that the stream file is parsed in pdf Format
Response. addHeader ("Content-Disposition", "attachment; filename = \" "+ filename +" \ ""); set the file name displayed during download to filename.
Why do we need to escape it here to prevent space in the file name from truncating the file name?
The rest is to push the file stream to the browser, which is not described here.