在开发项目中,有将图片存储到项目中的,也有将图片存储到nginx,或者文件服务器等等,这次要做的是读取本地硬盘上的图片信息,进行前端的回显,不啰唆,直接上代码吧。
1,跳转controller层,传递绝对路径的参数:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
public class ImageController {
@RequestMapping("/goIndex")
public String goToIndex(Model model) {
//根据条件读取数据库信息
model.addAttribute("url", "C:/Users/Public/Pictures/Sample Pictures/张1阿森纳.jpg");
return "showImage";
}
}
2.html层:高度和宽度什么的可以自己设置,用的是springboot中的thymeleaf(也可以自己写个js用attr(src,"自己拼接路径"))
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<img alt="" th:src="'/getImgByPath?path=' + ${url}" width="90px" height="90px"/>
</body>
</html>
3.调用controller层:
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;