Basic ideas:
The previous idea was to pop up a box to ask the user where he wanted to store the file, and then I generated another file and put it there. However, my idea did not succeed.
The way to download files is very simple to click on the link. The background outputs the file stream and realizes the download function through the browser, including inquiring about location and file storage. Most browsers will configure a fixed location, and may not ask every time.
The front-end is very simple, a <a> tag, href="backend method address". If your needs cannot be directly used in hyperlink, you can write it in js
window.location.href = "Backend Method Address".
After jumping to the background method
String filePath = this.getClass().getClassLoader().getResource("").toURI().getPath() + "/exportPdf.pdf"; //Path of the file in the project File outfile = new File(filePath); String filename = outfile.getName();//Get the file name InputStream fis = new BufferedInputStream(new FileInputStream(filePath)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); //Read file stream fis.close(); response.reset(); //Reset the result set response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1")); //Return the header file name response.addHeader("Content-Length", "" + outfile.length()); //Return the header file size response.setContentType("application/octet-stream"); //Set the data type//Get the return body output right OutputStream os = new BufferedOutputStream(response.getOutputStream()); os.write(buffer); // Output file os.flush(); os.close(); The browser will directly recognize this form of file output and a dialog box pops up.
Note that this method must be adjusted in the background using link method. It is not possible to use ajax and XMLHttpRequest methods. In this way, the returned file stream will be returned to the method's callback function. Of course, if you want to get the file in js, this will work.
Example
package cn.itcast.day06.web.servlet; import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder; import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Implement anti-theft chain function// Get the referer header to explain where the visitor comes from String referer = request.getHeader("referer"); if(referer==null || !referer.startsWith("http://localhost")) { // It is a link thief response.sendRedirect("/day06/index.jsp"); return ; } // Solve the problem of response Chinese garbled response response.setContentType("text/html;charset=utf-8"); // Set the encoding of the message body // The http response message header sent through the http protocol cannot appear in Chinese Chinese, Chinese must be url-encoded String filename = URLEncoder.encode("Beauty.jpg", "utf-8"); // Notify the browser to read the resource response.setHeader("content-disposition", "attachment;filename="+filename); // Read the image data and send it to the ie browser String webPath = "/download/Beauty.jpg"; // equivalent to the path of the current web application ServletContext servletContext = super.getServletContext(); InputStream in = servletContext.getResourceAsStream(webPath); OutputStream out = response.getOutputStream(); int len; byte[] buffer = new byte[1024]; while((len=in.read(buffer))!=-1) out.write(buffer, 0, len); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }