Discussions

General J2EE: Displaying Images( buffered data ) from the Database using Java

  1. Dear all, I was stored the gif/jpg image into the MySQL Database as a buffered image. But when i was retrive the image from database and display in jsp page, it will not working well. The image is not displaying. The content get from the database, but it's type is mismatch i think. Here are the code i was using. FOR UPLOADING IMAGE <% /* The Following Code is Used To Insert An Image Into Database */ String filename=""; try { //Download com.oreilly package MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024); Enumeration files=multi.getFileNames(); File f=null; while(files.hasMoreElements()) { String name=(String)files.nextElement(); filename=multi.getFilesystemName(name); String type=multi.getContentType(name); f=multi.getFile(name); System.out.println("The File is "+f); } Connection con=null; String userName="root"; String password = "veradis"; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample",userName,password); Statement stmt = con.createStatement(); InputStream is = new FileInputStream(f); byte b[]=new byte[is.available()]; is.read(b); int flag=0; try { String sql = "INSERT into tbl_image(image) values('" + b + "')"; System.out.println(sql); stmt.execute(sql); flag=1; } catch(Exception e) { System.out.println("SQL Exception : " + e); } if(flag==1) { System.out.println("Query Executed Successfully"); } stmt.close(); } catch(Exception e) { System.out.println(e); } out.println("The Image is Added into Database"); %> and FOR RETRIVE AND DISPLAY FROM THE DATABASE JSP Page <% try { javax.servlet.http.HttpServletResponse res=null;; int returnValue = 0; Connection conn = null; Statement stmt = null; ResultSet rs = null; InputStream in = null; OutputStream os = null; Blob blob = null; String text; text=request.getParameter("text"); Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","veradis"); final String query = "SELECT image FROM tbl_image"; conn.setAutoCommit(false); stmt = conn.createStatement(); rs = stmt.executeQuery(query); int i=1; if(rs.next()) { String len1 = rs.getString("image"); int len = len1.length(); byte [] b = new byte[len]; in = rs.getBinaryStream("image"); int index = in.read(b, 0, len); OutputStream outImej = new FileOutputStream("C:/Documents and Settings/Tamil/Desktop/photo/img"+i+".JPG"); while (index != -1) { outImej.write(b, 0, index); index = in.read(b, 0, len); System.out.println("=========================="); System.out.println(index); System.out.println(outImej); System.out.println("=========================="); } outImej.close(); i++; } else { returnValue = 1; } } catch(Exception e) { out.println("SQLEXCEPTION : " +e); } %> If i can run this code the image column was displayed as empty. If anyone can know how to solve this problem, please help me. Regards Tamilvanan
  2. You image is streaming from what's indicated in URL, which means you either put a dynamic link after you generate the file and put in your webapp/{your application}/image folder so when users click on the link it asks for the file from the generated file location, it displays. Or you could write a jsp or servlet that gets the image from database (supposed to be an array of bytes, put it into response.getOutputStream() and out to the client. This you still need to put a link in your JSP, but this link is actually the servlet's URL with parameter of the image name(or id) to retrieve the image in bytes for you and return it back to the browser.
  3. Hai, Thanks for your reply. It's working fine. I am getting the image from database, and write it as follows InputStream sImage; sImage = rs.getBinaryStream(1); response.reset(); response.setContentType(rs.getString("type")); response.getOutputStream().write(bytearray,0,size); Thanks for your advice. And then i need to Reduce the time when i upload the image and Retrive image from Database. Could you please help me for this. Thanks
  4. PLEASE SEND THE SOURCE CODE[ Go to top ]

    CAN YOU PLEASE SEND THE BOTH JSP PAGES' SOURCE CODE TO sarathjithan at gmail dot com PLEASE PLEASE PLEASE PLEASE.