Discussions

General J2EE: How to send images(like *.jpg) using JMS?

  1. How to send images(like *.jpg) using JMS? (2 messages)

    Hi friends:

             Does JMS could send images? I use JMS Point to Point pattern. I want to send some picture from client to JMS server. I want to know if it is possible. I use the following code, but it couldn't work. The Picture that I receieved can't be opened. My code is:

    [code]

    Client:

     File f=new File("C:/jmstest.jpg");
    byte[] buf = new byte[new Long(f.length()).intValue()];
            BytesMessage message = session.createBytesMessage();
            message.writeBytes(buf, 0, buf.length);
            
            InputStream in=new FileInputStream(f);
            while(in.read()!=-1){
             in.read(buf);
            }
            message .writeBytes(buf);
    sender.send(message , DeliveryMode.PERSISTENT,
                    Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);

    [/code]

    Server side:
    [code]
    QueueReceiver receiver = session.createReceiver(queue);
    receiver.setMessageListener(new MessageListener() {
    public void onMessage(Message message) {
    BytesMessage m=(BytesMessage)message;
    try {
    //byte[] bb=new byte[100000];
    byte[] bb=m.getJMSCorrelationIDAsBytes();
    File file = new File("d:/test.jpg");
    FileOutputStream to = new FileOutputStream(file);
    while(m.readByte()!=-1){
    to.write(bb);
    }
    System.out.println("Get f: "+bb);

    [/code]

    I can receieve the test.jpg file but it is broked . I mean that the test.jpg can't be opened. Is there any error in my code? Thks!
  2. This will work:

    publisher:
    ---
    File fileToPublish=new File("C:/jmstest.jpg");
    BytesMessage bm = session.createBytesMessage();
    InputStream in= new FileInputStream(fileToPublish);
    BufferedInputStream inBuf= new BufferedInputStream(in);
    int i;
    while((i=inBuf.read())!=-1){
       bm.writeInt(i);
    }
    //adding an eof
    bm.writeInt(-1);
    //send message

    subscriber (in the onMessage-method):
    ---
    BytesMessage bm = (BytesMessage)msg;
    File file = new File(absoluteFileName);
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream outBuf = new BufferedOutputStream(fos);
    int i;
    while((i=bm.readInt())!=-1){
       outBuf.write(i);
    }
    outBuf.close();
    fos.close();

    /pj
  3. Once we send file then it comes into broker but while receiving getting BytesMessage as a null

    Please suggest

    Receiver Code :

    BytesMessage bm = (BytesMessage)topicSubscriber.receiveNoWait(); // receive(1000);
        System.out.println("bm-->"+bm);
        if(bm !=null){
         File file = new File("D:/jms/myfie.txt");
         FileOutputStream fos = new FileOutputStream(file);
         BufferedOutputStream outBuf = new BufferedOutputStream(fos);
         int i;
         while((i=bm.readInt())!=-1){
            outBuf.write(i);
         }
         System.out.println("outBuf-->"+outBuf);
         System.out.println(file.isFile());
         outBuf.close();
         fos.close();
        }

     

    Sender code :

    File f=new File("D:/Import.txt");
              System.out.println("is File "+f.isFile());
              BytesMessage bm = topicSession.createBytesMessage();
              InputStream in= new FileInputStream(f);
              BufferedInputStream inBuf= new BufferedInputStream(in);
              int i;
              while((i=inBuf.read())!=-1){
                 bm.writeInt(i);
              }
              System.out.println("after while");
              //adding an eof
              bm.writeInt(-1);
              System.out.println("BM = "+bm);
              topicPublisher.send(bm);
              System.out.println("sent successfully");
              topicConnection.stop();