일부 이미지를 PC의 로컬 경로에 저장하고 파일 이름을 db에 저장했습니다. 이제 데이터베이스에서 모든 이미지를로드하고 자바 스크립트 및 thymemleaf를 사용하여 html 페이지에 표시하려고합니다. 브라우저가 로컬 리소스를로드 할 수 없다고 말했습니다. 그래서 스프링 부트로 서블릿을 만들었습니다. 그러나 그것은 이미지를 보여주지 않습니다.로컬 소스에서 동적 이미지로드 중
Servlet
@WebServlet("/admin/imgServlet")
public class AdminImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String urlImage = request.getParameter("admin");
response.setContentType("image/jpeg");
ServletOutputStream out;
out = response.getOutputStream();
FileInputStream fin = new FileInputStream("C:/Users/Public/Documents/" + urlImage);
BufferedInputStream bin = new BufferedInputStream(fin);
BufferedOutputStream bout = new BufferedOutputStream(out);
int ch = 0;
while ((ch = bin.read()) != -1) {
bout.write(ch);
}
bin.close();
fin.close();
bout.close();
out.close();
}
}
@Bean
public ServletRegistrationBean adminImageServlet() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(AdminImageServlet.class);
dispatcherServlet.setApplicationContext(applicationContext);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/admin/imgServlet");
servletRegistrationBean.setName("imgServlet");
return servletRegistrationBean;
}
로딩 이미지
자바 스크립트
'<img src=imgServlet?admin='+articles.bannerImg+' class="img-responsive" />'
오류 난 imgServlet:1 GET http://localhost/techtalks/admin/imgServlet?admin=pyjc4x0B.jpeg 404 (Not Found)
받고 있어요 그리고 나는 또한 thymeleaf HTML 페이지
에 로컬 리소스에서 이미지를로드 할model.addAttribute("localImg", "E:\\picz\\assasins.jpg");
스프링 컨트롤러는이 모델을 전달할 것이고, 이미지를 각각의 파일에 html 페이지로 전달할 것입니다.
<img th:src="@{${localImg}}"/>