您的位置:首页 > Web前端 > JavaScript

jsp中response实现图片下载

2014-02-25 22:19 776 查看
public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String path = this.getServletContext().getRealPath("/download/中国.png");

String filename=path.substring(path.indexOf("\\")+1);

response.setHeader("content-disposition", "attachment;filename="+RUL(filename,“UTF-8”));

FileInputStream in=null;

ServletOutputStream 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(IOException e){

e.printStackTrace();

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: