@RequestMapping(value = "/home/img/{imgname}/{imgtype}", method = RequestMethod.GET)
public void getImge(@PathVariable String imgname, @PathVariable String imgtype,
HttpServletRequest request, HttpServletResponse response){
BufferedInputStream in = null;
OutputStream out = null;
try {
File file = new File("/home/img/" + imgname+ "." + imgtype);
in = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(response.getOutputStream());
// 设置response内容的类型
response.setContentType(new MimetypesFileTypeMap().getContentType(file));
//设置响应头和客户端保存文件名
response.setHeader("Content-disposition", "attachment;filename=" + imgname + "." +imgtype);
byte[] buffer = new byte[10240];
int length = 0;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
out.flush();
} catch (IOException e) {
throw SOAException.error("获取图片失败", e);
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
throw SOAException.error("文件流未成功关闭", e);
}
}
}