相信很多小伙伴在做商城项目时会用到在前端展示商品的图片信息的功能,那么我们如何基于不把图片路径存储在数据库而查询出存储在Tomcat中图片路径的功能呢?下面我们来实现一下。
Controller层:
@RequestMapping("/getPicture.do")
protected void getPicture(String file, HttpServletRequest request,
HttpServletResponse response) throws IOException {
String StrFileName = file;
// 文件图片路径
String uploadPath = request.getServletContext().getRealPath("\\Temp");
File uploadDir = new File(uploadPath);
// 判断文件图片是否存在
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
// String uploadPath = "D:\\JavaTest\\LogisticsSystem\\Temp";
if (StrFileName!=null) {
File WenJian = new File(uploadPath + File.separator + StrFileName);
if (WenJi