HI,

I want to write the image file from a database to disk by using a queue. I can write these images on disk from the result set.

Can someone tell me where I am wrong in the following code? I get: "trying to write to disk: Closed Connection"

Thank you.

{code}

public class ExtractPicture implements Runnable{

 

 

    private BlockingQueue<InputStreamMessage> queue;

    public ExtractPicture(BlockingQueue<InputStreamMessage> queue){

        this.queue = queue;

    }

   

 

 

    @Override

    public void run() {

        try(Connection conn = new DatabaseConnection().getConnection();

            Statement stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery("select mypicture from testpicture")){           

            while(rs.next()){

//                System.out.println(rs.getInt("mypicture") + " <== added");

//                queue.put(rs.getInt("id"));

                InputStreamMessage ism = new InputStreamMessage(rs.getBinaryStream("mypicture"));

                try{

                    queue.put(ism);

                }catch(Exception e){

                    System.out.println(e.getMessage());

                }

               

            } 

        }catch(Exception e){

            System.out.println(e.getMessage());

        }

    }

}

 

 

class Consumer implements Runnable{

    private BlockingQueue<InputStreamMessage> queue;

    public Consumer(BlockingQueue<InputStreamMessage> queue){

        this.queue = queue;

    }

    @Override

    public void run() {

        try{           

            int z = 0;

            InputStreamMessage is;

//            (is = queue.take()) != null

 

 

            while((is = queue.take()) != null){

               

                System.out.println("consumer aa" + is.getInputStream());

//                writeToDisk(is.getInputStream(), "c:\\temp\\p" + z + ".jpeg");

                try{

                    int c = 0;

                    OutputStream f = new FileOutputStream(new File("c:\\temp\\p" + z + ".jpeg"));

                    while((c = is.getInputStream().read()) > -1 ){

                        f.write(c);

                    }

                    f.close();

                }catch(Exception exce){

                    System.out.println("trying to write to disk: " + exce.getMessage());

                }

                z++;

            }

 

 

        }catch(Exception e){

            System.out.println(e.getMessage());

        }

    }

}

 

 

class InputStreamMessage{

    private InputStream is;

    public InputStreamMessage(InputStream is){

        this.is = is;

    }

    public InputStream getInputStream(){

        return is;

    }

}

 

 

class RunService{

     public static void main(String[] args) {

 

 

        BlockingQueue<InputStreamMessage> queue = new ArrayBlockingQueue(10);

        ExtractPicture ep = new ExtractPicture(queue);

        Consumer c = new Consumer(queue);

        new Thread(ep).start();

        new Thread(c).start();

    }   

}

{code}

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners