This discussion is archived
6 Replies Latest reply: Nov 22, 2012 12:48 AM by Tolls RSS

ImageIO.read issue

bip_monkey Newbie
Currently Being Moderated
I have the following code:
package silli.oracle.apps.ap.storejournal.schema.server;

import java.awt.Image;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.io.OutputStream;
import java.io.InputStream;
import oracle.apps.fnd.common.WebAppsContext;
import oracle.apps.fnd.common.WebRequestUtil;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import java.sql.Blob;

import javax.imageio.ImageIO;


import javax.imageio.stream.MemoryCacheImageInputStream;

import javax.swing.ImageIcon;

import oracle.jdbc.pool.OracleDataSource;

public class ReadImage extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
    private Connection conn; 
    private Statement stmt; 
    private String trace; 
    int i=0; 

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try
        {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        }
        catch (Exception exception ) 
        { 
        exception.printStackTrace(); 
        throw new UnavailableException(exception.getMessage()); 
        } 
    }

    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response) throws ServletException, 
                                                           IOException {
        String Ukey = "";
        //response.setContentType(CONTENT_TYPE);
         String fileName = request.getParameter("fileName");
         response.setContentType("application/octet-stream");
         response.setHeader("Pragma", "No-cache");
         response.setHeader("Cache-Control", "no-cache");
         response.setDateHeader("Expires",0);
         response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

       /* PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>ReadImage</title></head>");
        out.println("<body>");
        out.println("<p>The servlet has received a GET. This is the reply.</p>");
        out.println("</body></html>");
        out.close();*/
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, 
                       HttpServletResponse response) throws ServletException, 
                                                            IOException {
        String Ukey = "";
        response.setContentType(CONTENT_TYPE);
        /*PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>ReadImage</title></head>");
        out.println("<body>");
        out.println("<p>The servlet has received a POST. This is the reply.</p>");
        out.println("</body></html>");
        out.close();*/
        
        trace = ""; 
        if (conn == null)
        {
        try
        {
        WebAppsContext ctx = null;               
        ctx = WebRequestUtil.validateContext(request, response);
        ctx = WebRequestUtil.createWebAppsContext(request, response);
        //conn = ctx.getJDBCConnection();
        System.out.println("I am in Read Image servlet : user name is  "+ ctx.getUserName()+"-"+ctx.getUserPwd()); 
        
        
        conn = ctx.getJDBCConnection();
        stmt = conn.createStatement();
        }
        catch (Exception exception ) 
        { 
        exception.printStackTrace(); 
        throw new UnavailableException(exception.getMessage()); 
        } 
        }
       //-- 
        try 
        { 
        String image_id = request.getParameter("id");
        // String image_id = "31966744";
        System.out.println("I am in Read Image servlet : image id is "+ image_id );
        if (image_id != null)
        {
        String sql = "SELECT TRANS_IMAGE FROM STORE_TRANS_JOURNAL_TEXT_V WHERE UKEY = " + image_id;  

        
        System.out.println("I am in Read Image servlet1 : image id is "+ image_id + "Sql is:"+ sql );
        ResultSet rs = stmt.executeQuery(sql); 
        System.out.println("I am in Read Image servlet 2: image id is "+ image_id + "After executing the query:");
        rs.next(); 
        System.out.println("I am in Read Image servlet 3: image id is "+ image_id + "after rs.next ():");
        InputStream in = null; 
        System.out.println("I am in Read Image servlet 3: image id is "+ image_id + "After instantiating the input stream:");        
        in = rs.getBinaryStream("TRANS_IMAGE"); 
        System.out.println("I am in Read Image servlet 4: image id is "+ image_id + "After getBinaryStream():");
        response.reset();
        System.out.println("I am in Read Image servlet 4.1: image id is "+ image_id + "After response.reset():");
        OutputStream out1 = response.getOutputStream(); 
        //OutputStream out1 = response.getOutputStream("C:\"); 
        System.out.println("I am in Read Image servlet 5: image id is "+ image_id + "After outputstream:");
        int b;       
      // FileInputStream fis = new FileInputStream("C:\\JDEV\\jdevhome\\jdev\\myhtml\\OA_MEDIA\\US\\out.gif");
       

       System.out.println("I am in Read Image servlet 5: image id is "+ image_id + "After outputstream:");
       //BufferedWriter out = new BufferedWriter(fstream);
              
        while ((b = in.read()) != -1) 
        { 
        out1.write(b);        
        }       
        System.out.println("I am in Read Image servlet 6: image id is "+ image_id + "After the while loop:");
        in.close();       
        System.out.println("I am in Read Image servlet 7: image id is "+ image_id + "After the in.close():");
               //  out1.flush(); 
        System.out.println("I am in Read Image servlet 8: image id is "+ image_id + "After the outl.flush():");
        out1.close();        
        System.out.println("I am in Read Image servlet 6: image id is "+ image_id + "After the outl.close():");
        //---------------
        
         Blob test=rs.getBlob("TRANS_IMAGE");  
         response.setContentType("image/jpeg");
         response.setContentLength( (int) test.length());
      

         int blobLength = (int) test.length();   
         byte[] blobAsBytes = test.getBytes(1, blobLength);   
        //byte[] blobAsBytes = test.getBytes();  
        //String currentEncoding = System.getProperty("file.encoding");
        
        
        

         System.out.println("Length"+blobLength);             
         System.out.println("testlen"+blobAsBytes.length);   
         
         
         System.out.println("buffer available"+new ByteArrayInputStream( blobAsBytes ).available());   
         
        System.out.println("Available image readers1: "+ImageIO.getImageReadersByFormatName("jpg"));
        
        System.out.println("Available image readers2: "+ImageIO.getImageReadersByFormatName("JPEG"));
        
        System.out.println("Available image readers3: "+ImageIO.getImageReadersByFormatName("jpeg"));
         
         
         BufferedImage image = ImageIO.read( new ByteArrayInputStream( blobAsBytes ) ); 
         // BufferedImage image = new ImageIcon(Toolkit.getDefaultToolkit().createImage(blobAsBytes));

        
         System.out.println("Buffered image width and height: "+image.getWidth()+"-"+image.getHeight());
         
             ImageIO.write(image, "JPEG", new File("C:\\JDEV\\jdevhome\\jdev\\myhtml\\OA_MEDIA\\US\\image.JPG"));           
        
           
            //ImageIO.write(image, "JPEG", new File("C:\\sams1\\image.JPG"));   
            //   ImageIO.write(image, "PNG", new File("C:\\sams1\\image.PNG"));   
            //   ImageIO.write(image, "BMP", new File("C:\\sams1\\image.BMP"));   
           //    ImageIO.write(image, "TIF", new File("C:\\sams1\\image.TIF"));   
         System.out.println("row"+i);              
         i++; 
        //---------------
        
         stmt.close();
        }
        } 
        catch (SQLException sqlException)
        { 
        sqlException.printStackTrace(); 
        } 
        catch (Exception e) 
        {
        e.printStackTrace(); 
        throw new ServletException("Fatal Error" + e.getMessage()); 
        } 
        }
    

    public void doPut(HttpServletRequest request, 
                      HttpServletResponse response) throws ServletException, 
                                                           IOException {
        String Ukey = "";
    }
    
    
}
It is throwing an NPE at the line
BufferedImage image = ImageIO.read( new ByteArrayInputStream( blobAsBytes ) ); 
Why is not able to read the image from the blob column? Pls. note that I am not sure of the type of the image stored in the blob column. It is loaded by an external legacy source. As far as I know it is a (oracle compressed) jpeg.

Thanks,

Edited by: OAF_Monkey on Nov 21, 2012 2:06 PM
  • 1. Re: ImageIO.read issue
    gimbal2 Guru
    Currently Being Moderated
    You get a NullPointerException. You get that exception when the application tries to use a null-reference. If it really happens on the line you say, then I guess the blobAsBytes object is actually null.

    What would be really useful is to see the exception stacktrace that Java so graciously provided to you.
  • 2. Re: ImageIO.read issue
    bip_monkey Newbie
    Currently Being Moderated
    I aplogize I am a java newbie (learning on the fly...)

    Below is the trace (I think this is what you are referring to...) with my SOP messages:

    12/11/21 14:33:53 I am in Read Image servlet 2: image id is 31966752After executing the query:
    12/11/21 14:33:53 I am in Read Image servlet 3: image id is 31966752after rs.next ():
    12/11/21 14:33:53 I am in Read Image servlet 3: image id is 31966752After instantiating the input stream:
    12/11/21 14:33:53 I am in Read Image servlet 4: image id is 31966752After getBinaryStream():
    12/11/21 14:33:53 I am in Read Image servlet 4.1: image id is 31966752After response.reset():
    12/11/21 14:33:53 I am in Read Image servlet 5: image id is 31966752After outputstream:
    12/11/21 14:33:53 I am in Read Image servlet 5: image id is 31966752After outputstream:
    12/11/21 14:33:53 I am in Read Image servlet 6: image id is 31966752After the while loop:
    12/11/21 14:33:53 I am in Read Image servlet 7: image id is 31966752After the in.close():
    12/11/21 14:33:53 I am in Read Image servlet 8: image id is 31966752After the outl.flush():
    12/11/21 14:33:53 I am in Read Image servlet 6: image id is 31966752After the outl.close():
    *12/11/21 14:33:53 Length4120*
    *12/11/21 14:33:53 testlen4120*
    *12/11/21 14:33:53 buffer available4120*
    12/11/21 14:33:53 Available image readers1: javax.imageio.ImageIO$ImageReaderIterator@a50ea1
    12/11/21 14:33:53 Available image readers2: javax.imageio.ImageIO$ImageReaderIterator@63769d
    12/11/21 14:33:53 Available image readers3: javax.imageio.ImageIO$ImageReaderIterator@6edb
    12/11/21 14:33:53 java.lang.NullPointerException
    12/11/21 14:33:53      at silli.oracle.apps.ap.storejournal.schema.server.ReadImage.doPost(ReadImage.java:202)
    12/11/21 14:33:53      at silli.oracle.apps.ap.storejournal.schema.server.ReadImage.doGet(ReadImage.java:90)
    12/11/21 14:33:53      at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
    12/11/21 14:33:53      at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    12/11/21 14:33:53      at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:713)
    12/11/21 14:33:53      at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
    12/11/21 14:33:53      at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:871)
    12/11/21 14:33:53      at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:453)
    12/11/21 14:33:53      at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:221)
    12/11/21 14:33:53      at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:122)
    12/11/21 14:33:53      at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:111)
    12/11/21 14:33:53      at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
    12/11/21 14:33:53      at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
    12/11/21 14:33:53      at java.lang.Thread.run(Thread.java:595)

    Highlighted lines shown that the "blobAsBytes.length" and the available...
    That would mean that the "blobAsBytes" is not null and has content with in that....correct??

    Thanks,

    Edited by: OAF_Monkey on Nov 21, 2012 2:43 PM
  • 3. Re: ImageIO.read issue
    gimbal2 Guru
    Currently Being Moderated
    Yep, nothing special in there so there is no magic going on. Something on line 202 of the class is causing an NPE. So check out that line and try to reason for yourself what can be null because Java is not lying to you.

    Of course you have to be sure what line 202 is. You wouldn't be the first one to deploy a class, add some lines in the source file and then forget to redeploy, making the line number that Java reports and the source file be out of sync.
  • 4. Re: ImageIO.read issue
    bip_monkey Newbie
    Currently Being Moderated
    After looking at the line 202 and commenting out:
     System.out.println("Buffered image width and height: "+image.getWidth()+"-"+image.getHeight());
    getting the following
    12/11/21 14:55:47 buffer available4120
    12/11/21 14:55:47 Available image readers1: javax.imageio.ImageIO$ImageReaderIterator@469bc
    12/11/21 14:55:47 Available image readers2: javax.imageio.ImageIO$ImageReaderIterator@1b9e5dc
    12/11/21 14:55:47 Available image readers3: javax.imageio.ImageIO$ImageReaderIterator@1a209df
    *12/11/21 14:55:47 java.lang.IllegalArgumentException: im == null!*
    12/11/21 14:55:47      at javax.imageio.ImageIO.write(ImageIO.java:1429)
    12/11/21 14:55:47      at javax.imageio.ImageIO.write(ImageIO.java:1488)
    12/11/21 14:55:47      at silli.oracle.apps.ap.storejournal.schema.server.ReadImage.doPost(ReadImage.java:204)
    12/11/21 14:55:47      at silli.oracle.apps.ap.storejournal.schema.server.ReadImage.doGet(ReadImage.java:90)
    12/11/21 14:55:47      at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
    12/11/21 14:55:47      at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    12/11/21 14:55:47      at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:713)
    12/11/21 14:55:47      at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
    12/11/21 14:55:47      at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:871)
    12/11/21 14:55:47      at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:453)
    12/11/21 14:55:47      at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:221)
    12/11/21 14:55:47      at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:122)
    12/11/21 14:55:47      at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:111)
    12/11/21 14:55:47      at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
    12/11/21 14:55:47      at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
    12/11/21 14:55:47      at java.lang.Thread.run(Thread.java:595)


    Although the NPE is gone....when the
      ImageIO.write(image, "JPEG", new File("C:\\JDEV\\jdevhome\\jdev\\myhtml\\OA_MEDIA\\US\\image.JPG"));  
    executes, it creates the file image.JPG....however, the file is '0' byte. I am trying to get the file with actual image in it.

    Any ideas/suggestions why it is a 0byte??

    Thanks,
  • 5. Re: ImageIO.read issue
    gimbal2 Guru
    Currently Being Moderated
    OAF_Monkey wrote:
    Any ideas/suggestions why it is a 0byte??
    Yes, something is wrong. And now you have to go in and investigate what that might be. It might be required for you to dig into articles about ImageIO and JDBC blob handling.

    You're a dev: writing code is actually a small part of your job. Researching and investigating issues is a larger part of it. This is your opportunity to get better at it, but you have to make the effort. You're not going to off-load the responsibility to a forum, you have to do it. But when you have specific questions that might help you to research this problem, feel free to ask them.
  • 6. Re: ImageIO.read issue
    Tolls Journeyer
    Currently Being Moderated
    OAF_Monkey wrote:
    After looking at the line 202 and commenting out:
    System.out.println("Buffered image width and height: "+image.getWidth()+"-"+image.getHeight());
    But that only removes the immediate NPE.
    When you get an NPE you need to figure out what is null, and then figure out why it's null.
    Deleting lines to remove exceptions will simply result in an application that does nothing.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points