1 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 2 String realPath = "C:\\CS\\Java\\IdeaProjects\\JavaWebLearn\\practice01\\src\\main\\resources\\csgo.png";//绝对路径写法 3 4 String path = realPath.substring(realPath.lastIndexOf("\\")); 5 6 resp.setHeader("Content-Disposition", "attachment;filename" + path); 7 8 FileInputStream in = new FileInputStream(realPath); 9 int len = 0; 10 byte[] buffer = new byte[1024]; 11 12 ServletOutputStream out = resp.getOutputStream(); 13 14 while ((len = in.read(buffer)) != -1) { 15 out.write(buffer,0,len); 16 } 17 18 out.close(); 19 in.close(); 20 }