本文共 1378 字,大约阅读时间需要 4 分钟。
配置web.xml
DownServlet01 cn.downservlet.DownServlet01 filePath d:\简历.pdf
java:
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DownServlet01 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // NO1 ServletConfig config = this.getServletConfig(); String filePath = config.getInitParameter("filePath"); // NO2 头信息 File file = new File(filePath); response.setHeader("content-disposition", "attachment;filename=" + file.getName()); // NO3 InputStream is = new FileInputStream(file); OutputStream os = response.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) > 0) { os.write(buf, 0, len); } is.close(); os.close(); }}
转载于:https://blog.51cto.com/357712148/2104500