1. 程式人生 > >jsp中response實現圖片下載

jsp中response實現圖片下載

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();
}
}
}


}