Download the site image code and parse the garbled code

  • 2021-01-02 21:57:33
  • OfStack

 
//  Get site objects  
ServletContext context = this.getServletContext(); 
//  Access to website resources  
String path = context.getRealPath("/imgs/ people .jpg"); 
File file = new File(path); 
System.out.println(file); 
//  Set the response header to inform the browser how the data will be processed  
response.setHeader("content-disposition", 
"attachment;filename="+ 
URLEncoder.encode(file.getName(),"utf-8")); //  Handle filename scramble to specify image format for download  
//  Specifies the byte input stream object  
FileInputStream in = new FileInputStream(file); 
//  Gets a byte output stream object  
ServletOutputStream out = response.getOutputStream(); 
//  Read and write  
byte [] b = new byte[1024]; 
int len = 0; 
while((len = in.read(b)) != -1){ 
out.write(b, 0, len); 
} 
//  Release resources  
in.close(); 

Related articles: