上篇文章写到:基于maven的图片上传,这篇文章将介绍如何将上传后的图片显示出来!(我上传的图片在E:\test\文件夹下)具体代码如下:
public static final String IMG_DIR = "E:"+File.separator+"test"+File.separator;
@RequestMapping("/getAvatar/{id}")
public void getAvatar(@PathVariable("id") String id, HttpSession session, HttpServletResponse response) {
FileInputStream fis = null;
ServletOutputStream outputStream = null;
User user1 = userService.selectById(id);
String imgName = user1.getAvatar();
if (imgName == null) {
imgName = IMG_DIR+"qq.jpg";
}
try {
File imgFile;
if (StringUtils.isNotEmpty(imgName)) {
outputStream = response.getOutputStream();
User user = (User) session.getAttribute("user");
imgFile = new File( imgName);
if (!imgFile.exists()) {
imgFile = new File(imgName);
}
} else {
imgFile = new File(imgName);
}
//System.out.println(imgFile);
fis = new FileInputStream(imgFile);
byte[] b = new byte[1024];
while ((fis.read(b)) != -1) {
outputStream.write(b);
}
} catch (IOException e) {
logger.error("{}", e);
} catch (Exception e) {
logger.error("{}", e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
具体逻辑以及图片路径、名称,请根据自己的存储路径修改!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/96858.html