The structure of the webapp project is as follows:
The contents of the download.html file are as follows:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body> <h1> Resource download:</h1> <p> When simply using the a tag, only files that cannot be parsed by the browser will be downloaded, otherwise they will be parsed directly by the browser. </p> <a href="/WEB/resource/a.mp3" rel="external nofollow" >a.mp3</a><br> <a href="/WEB/resource/a.exe" rel="external nofollow" >a.exe</a><br> <a href="/WEB/resource/a.txt" rel="external nofollow" >a.txt</a><br> <a href="/WEB/resource/a.xlsx" rel="external nofollow" >a.xlsx</a><br> <a href="/WEB/resource/a.png" rel="external nofollow" >a.png</a><br> <p>So, using the a tag combined with the servlet response to instruct the browser not to parse these files to be downloaded</p> <a href="/WEB/download?filename=a.mp3" rel="external nofollow" >a.mp3</a><br> <a href="/WEB/download?filename=a.exe" rel="external nofollow" >a.exe</a><br> <a href="/WEB/download?filename=a.txt" rel="external nofollow" >a.txt</a><br> <a href="/WEB/download?filename=a.xlsx" rel="external nofollow" >a.xlsx</a><br> <a href="/WEB/download?filename=a.png" rel="external nofollow" >a.png</a><br></body></html>
The contents of the downloaded Servlet-download.java file are as follows:
package com.download.servlet;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class Download */public class Download extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. Get the file name requested to download String filename = request.getParameter("filename"); //2. Get the file system path of the file String filePath = request.getServletContext().getRealPath("resource/"+filename); //3. Set the response header to prompt the browser not to parse the response file data, but to parse it in the form of attachment, that is, the download function response.setContentType(this.getServletContext().getMimeType(filename)); response.setHeader("Content-Disposition", "attachment;filename="+filename); //4. Read the file input stream and the response output stream, and output the data to the client InputStream in = new FileInputStream(filePath); ServletOutputStream out = response.getOutputStream(); int len = 0; byte[] buf = new byte[1024]; while((len=in.read(buf))!=-1) { out.write(buf, 0, len); } in.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}Enter http://localhost:8080/DownloadServlet/download.html in the browser address bar.
The above example explanation of the file download function provided by java web response is all the content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.