300.30 response实现文件下载

来源:互联网 发布:logitech g502 mac 编辑:程序博客网 时间:2024/06/05 22:59
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String path = this.getServletContext().getRealPath("/download/卡卡.jpg");String filename = path.substring(path.lastIndexOf("\\")+1);response.setHeader("content-disposition", "attachment; filename="+URLEncoder.encode(filename,"utf-8"));InputStream in = new FileInputStream(path);OutputStream out = response.getOutputStream();int len = 0;byte[] buffer = new byte[1024];while((len = in.read(buffer))!= -1){out.write(buffer,0,len);}out.flush();in.close();out.close();}

原创粉丝点击