spring boot - loading dynamic image from local source -
i have saved images local path of pc , saved file name db. want load image db , show html page using java script , thymemleaf. browser says not allowed load local resources , made servlet in spring boot. not showing image.
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; }
java script loading image
'<img src=imgservlet?admin='+articles.bannerimg+' class="img-responsive" />'
error i'm getting imgservlet:1 http://localhost/techtalks/admin/imgservlet?admin=pyjc4x0b.jpeg 404 (not found)
and want load image local resources thymeleaf html page
model.addattribute("localimg", "e:\\picz\\assasins.jpg");
spring controller pass model , pass image respective file html page this.
<img th:src="@{${localimg}}"/>
your servlet code works me. able image successfully.
i presume problem file name. please input file name below (i.e. without 'e' in jpeg).
it should work if file present in local path referring.
pyjc4x0b.jpg
Comments
Post a Comment