java.png
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通過路徑得到一個輸入流
String path = this.getServletContext().getRealPath("/WEB-INF/classes/欣欣.JPG");
FileInputStream File = new FileInputStream(path);
//創建字節輸出流
ServletOutputStream sos = response.getOutputStream();
//得到要下載的文件名
String fileName = path.substring(path.lastIndexOf("\\")+1);
//設置文件名的編碼
fileName = URLEncoder.encode(fileName, "UTF-8");
//告知客戶端要下載文件
response.setHeader("content-disposition", "attachment;fileName-"+fileName);
response.setHeader("content-type", "image/jpeg");
//執行輸出操作
int len = 1;
byte[] b = new byte[1024];
while ((len=File.read(b))!=-1){
sos.write(b,0,len);
}
File.close();
}