protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String realPath = getServletContext().getRealPath("/img/hm.png"); BufferedInputStream b = new BufferedInputStream(new FileInputStream(realPath)); //Content-type:消息头名称 支持的类型 //application/octet-stream:消息头参数,应用的类型为字节流 resp.setHeader("Content-Type","application/octet-stream"); //Content-Disposition,响应为下载 //attachment 附件形式 resp.setHeader("Content-Disposition","attachment;filename=hm.png"); ServletOutputStream outputStream = resp.getOutputStream(); byte[] bytes = new byte[1024]; int len; while ((len=b.read(bytes))!=-1){ outputStream.write(bytes,0,len); } }
Content-type:消息头名称 支持的类型
application/octet-stream:消息头参数,应用的类型为字节流
Content-Disposition,响应为下载
attachment 附件形式