java实现图片下载

try {
String filename=“tt.jpg”;
//当文件名不是英文名的时候,最好使用url解码器去编码一下,
filename=URLEncoder.encode(filename,“UTF-8”);
//将响应的类型设置为图片
response.setContentType(“image/jpeg”);
response.setHeader(“Content-Disposition”, “attachment;filename=” + filename);
File file = new File(“E:\tt.jpg”);
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream out = response.getOutputStream();
int len = 0;
byte[] by = new byte[1024*10];
while ((len = fileInputStream.read(by))>0){
out.write(by,0,len);
}
out.close();
fileInputStream.close();
}catch (IOException e){
e.printStackTrace();
}

你可能感兴趣的:(杂项)