title: loading dynamic image from local source
body: <p>I have saved some images to local path of pc and saved the file name to db. Now i want to load all the image from db and show to html page using java script and thymemleaf. Browser says not allowed to load local resources , So i made a servlet in spring boot. But it does not showing image.</p>
  
  <p><code>Servlet</code></p>
  
  <pre><code>@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;
      }
  </code></pre>
  
  <p>Java script for loading image </p>
  
  <pre><code>'&lt;img src=imgServlet?admin='+articles.bannerImg+'  class="img-responsive" /&gt;'
  </code></pre>
  
  <p>error i'm getting  <code>imgServlet:1 GET http://localhost/techtalks/admin/imgServlet?admin=pyjc4x0B.jpeg 404 (Not Found)</code></p>
  
  <p>And i also want to load image from local resources to thymeleaf html page</p>
  
  <p><code>model.addAttribute("localImg", "E:\\picz\\assasins.jpg");</code> spring controller will pass the Model like this and it will pass the image to respective file to html page like this.</p>
  
  <p><code>&lt;img th:src="@{${localImg}}"/&gt;</code></p>