response实现文件下载
2014-09-09 22:32
295 查看
假设web目录下有一个文件
程序如下
如果文件名是中文要用url编码,改为
response.setHeader("content-disposition", "attachment;filename"+URLEncoder.encode(filename,"utf-8"));
程序如下
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = this.getServletContext().getRealPath("/download/1.jpg"); String filename = path.substring(path.lastIndexOf("\\")+1); response.setHeader("content-disposition", "attachment;filename"+filename); InputStream in = null; OutputStream out = null; try{ in = new FileInputStream(path); int len = 0; byte buffer[] = new byte[1024]; out = response.getOutputStream(); while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } }finally{ if(in!=null){ try{ in.close(); }catch (Exception e){ e.printStackTrace(); } } if(out!=null){ try{ in.close(); }catch (Exception e){ e.printStackTrace(); } } } }
如果文件名是中文要用url编码,改为
response.setHeader("content-disposition", "attachment;filename"+URLEncoder.encode(filename,"utf-8"));
相关文章推荐
- php download.php实现代码 跳转到下载文件(response.redirect)
- 使用response实现文件的下载
- 使用response实现文件下载功能
- 用Response实现文件下载
- php download.php实现代码 跳转到下载文件(response.redirect)
- 利用response和HttP中的Content-Disposition: attachment; filename=aaa.zip响应头,实现简单的文件下载
- JSP隐含对象response实现文件下载的两种方式
- JSP隐含对象response实现文件下载的两种方法
- JSP隐含对象response实现文件下载的两种方法
- Response实现文件下载
- Response对象、HttpServletResponse简介、处理字节编码问题、实现动态文件下载
- Response实现文件下载
- response实现文件下载
- response实现文件下载
- Response实现文件下载
- 心得6--Response介绍和如何实现动态文件内容的下载详细介绍
- 心得6--Response介绍和如何实现动态文件内容的下载详细介绍
- 好记性不如烂笔头13-使用HttpServletResponse实现文件下载
- java实现文件下载的工具类-----------什么都可以下载,只要传入response和文件路径 两个参数
- Response对象、HttpServletResponse简介、处理字节编码问题、实现动态文件下载