1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| @WebServlet("/Responsedemo4") public class demo4 extends HttpServlet{ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{ String filename = "操作系统.md"; String path = "C:/Users/Retur0/Desktop/" + filename; filename = URLEncoder.encode(filename, "utf-8"); resp.setHeader("Content-disposition", "attachment;filename=" + filename);
FileInputStream fis = new FileInputStream(path); ServletOutputStream sos = resp.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while((len=fis.read(buf)) != -1) { sos.write(buf, 0, len); } fis.close(); } }
|