controller
// 扫描指定目录下的图片进行展示 @RequestMapping("/showPics") public ModelAndView showPics(ModelAndView mv) { // 图片存放路径 String picPath = Constant.CONFIG_PROPERTIES.getProperty("download.path"); // 获取图片 List<File> picList = MyFileUtils.listFilesBySuffixs(picPath, Constant.PIC_SUFFIXS, true); mv.setViewName("showPics"); mv.addObject("picList", picList); return mv; }
配置文件(application.properties)
# 本地图片
spring.mvc.static-path-pattern=/image/**
spring.resources.static-locations=file:C://Temp/pic/
配置图片目录地址映射。
视图(showPics.ftl)
<!DOCTYPE html> <html> <head> <title>显示所有图片</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> </head> <body> <div class="container"> <div class="row"> <div class="col-md-4"> <div class="panel panel-primary"> <div class="panel-heading text-center"> <span class="panel-title">显示所有图片</span> </div> <#list picList as pic> <br> <div class="panel-body"> <#--<img src="./image/zz.png" width="400" height="50"/>--> <img src="./image/${pic.name}" width="50%" height="50%"/> </div> </#list> </div> </div> </div> </div> </body> </html>
访问 http://localhost:8080/showPics 即可。