您的位置:首页 > 编程语言 > Java开发

springMVC下的通过<img src="">标签的src访问图片

2016-12-05 18:10 344 查看
图片的回显,在src中放入图片的回显路径

/**
* 百度ueditor编辑器的图片回显 通过<img/>标签中的src访问 例:http://localhost:8080/qxzst-sp-apis//pc/image/getUeditorImg/1480504795791062668/png
* @param imgname 图片名
* @param imgType 扩展名
* @param response
*/
@RequestMapping(value = "getUeditorImg/{imgname}/{imgType}", method = RequestMethod.GET)
public void getUeditorImg(@PathVariable String imgname, @PathVariable String imgType,
HttpServletResponse response) {
if ("null".equals(imgname) || "null".equals(imgType) || StringUtils.isBlank(imgname)
|| StringUtils.isNotBlank(imgType)) {
return;
}
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String timeStr = imgname.substring(0, imgname.length() - 6);
String dateFile = df.format(new Date(Long.parseLong(timeStr)));
File file = new File(UEDITOR_IMG_PATH + "/" + dateFile + "/" + imgname + "." + imgType);
in = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(response.getOutputStream());
response.setContentType(new MimetypesFileTypeMap().getContentType(file));// 设置response内容的类型
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) {
getLogger().error(e.getMessage(), e);
writeError500(response, e);
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: