How to read image bytes in java? -


i trying display image in jsp page using bellow code.everything fine error displayed , don't know how fix can 1 me fix this.

code:

  <%@page import="java.awt.image.bufferedimage"%> <%@page import="javax.imageio.imageio"%> <%@page import="java.io.*"%> <%@ page language="java" contenttype="text/html; charset=iso-8859-1"     pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>insert title here</title> </head> <body> <% inputstream instream = null; bufferedinputstream bis = null; bufferedimage bimage=null;  instream = new fileinputstream("/home/anand/desktop/encrypt/firstbg.jpg"); bis = new bufferedinputstream(instream); int numbyte = bis.available(); byte[] buf = new byte[numbyte]; bis.read(buf, 2, 3);    bimage = imageio.read(new bytearrayinputstream(buf));//give path of image bytearrayoutputstream baos = new bytearrayoutputstream(); imageio.write( bimage, "jpg", baos ); baos.flush(); byte[] imageinbytearray = baos.tobytearray(); baos.close(); string b64 = javax.xml.bind.datatypeconverter.printbase64binary(imageinbytearray); %>  <div>     <p>as of v6, java se provides jaxb</p>     <img src="data:image/jpg;base64, <%=b64%>" alt="visruth.jpg not found" /> </div>           </body> </html> 

error

severe: servlet.service() servlet [jsp] in context path [/get] threw exception [an exception occurred processing jsp page /newfile2.jsp @ line 27  24:  25:  bimage = imageio.read(new bytearrayinputstream(buf));//give path of image 26: bytearrayoutputstream baos = new bytearrayoutputstream(); 27: imageio.write( bimage, "jpg", baos ); 28: baos.flush(); 29: byte[] imageinbytearray = baos.tobytearray(); 30: baos.close();   stacktrace:] root cause java.lang.illegalargumentexception: image == null!     @ javax.imageio.imagetypespecifier.createfromrenderedimage(imagetypespecifier.java:925)     @ javax.imageio.imageio.getwriter(imageio.java:1591)     @ javax.imageio.imageio.write(imageio.java:1578)     @ org.apache.jsp.newfile2_jsp._jspservice(newfile2_jsp.java:90)     @ org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70)     @ javax.servlet.http.httpservlet.service(httpservlet.java:731)     @ org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:439)     @ org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:395)     @ org.apache.jasper.servlet.jspservlet.service(jspservlet.java:339)     @ javax.servlet.http.httpservlet.service(httpservlet.java:731)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208)     @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208)     @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220)     @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122)     @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:505)     @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:170)     @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103)     @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:957)     @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116)     @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:423)     @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1079)     @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:620)     @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:316)     @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145)     @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615)     @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61)     @ java.lang.thread.run(thread.java:745) 

i know problem writing bytes in bufferedimage don't know how fix this.


Comments