Copy codeThe Code is as follows:
// Obtain the website object
ServletContext context = this. getServletContext ();
// Obtain website resources
String path = context. getRealPath ("/imgs/ .jpg ");
File file = new File (path );
System. out. println (file );
// Set the response header to notify the browser of how to process the data
Response. setHeader ("content-disposition ",
"Attachment; filename =" +
URLEncoder. encode (file. getName (), "UTF-8"); // process garbled file names and specify the image format as download
// Specify the byte input stream object
FileInputStream in = new FileInputStream (file );
// Get the 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 ();