[rabbitmq-discuss] binary data corrupts when transferring on Rabbitmq

saurabh256 saurabh256 at yahoo.com
Mon Dec 17 05:49:30 GMT 2012


I am new to RabbitMQ and tried to transfer a word document but seems when
recieving or sending something happened during encoding or character set and
output file gets corrupted. 

Below is the code for consumer and publisher. Can someone tell me where I am
wrong? I am using RabbitMQ Version 3.0.0 on windows machine (if the
information helps).

I created one word doc manually and exported locally using the producer
which is working fine but document created by the consumer is corrupt and
word opens that asking for fix. 

This small sample is vey important for me as my all other use cases are
based on same foundation. 

--------------------------------
Publisher-----------------------

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class BaseProducer {

  private final static String QUEUE_NAME = "hello";

  public static void main(String[] argv) throws Exception {
      	      
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "Hello World!";
    
    String INPUT_FILE_NAME = "C:\\Folder\\Saurabh\\BSM\\video\\test.docx";
    byte[] fileContents = read(INPUT_FILE_NAME);
    
    AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder();
    AMQP.BasicProperties persistentBasic
    =
bob.priority(0).contentType("application/octet-stream").contentEncoding("UTF-8").build();
    
    channel.basicPublish("", QUEUE_NAME, persistentBasic,fileContents);
    System.out.println(" [x] Sent '" + message + "'");
    
    writeFile(fileContents);
    channel.close();
    connection.close();
  }
  
  static byte[]  read(String aInputFileName){
      //log("Reading in binary file named : " + aInputFileName);
      File file = new File(aInputFileName);
      //log("File size: " + file.length());
      byte[] result = new byte[(int)file.length()];
      try {
        InputStream input = null;
          int totalBytesRead = 0;
          input = new FileInputStream(aInputFileName);
          input.read(result);
          input.close();
          
        
      }
      catch (FileNotFoundException ex) {
       // log("File not found.");
      }
      catch (IOException ex) {
       // log(ex);
      }
      return result;
    }
  
  static void writeFile(byte[] result){
	  // write file from here only and see if working
      FileOutputStream out;
	try {
		out = new
FileOutputStream("C:\\Folder\\Saurabh\\BSM\\video\\test-local.docx");
		out.write(result);
	    out.close();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}  
	 catch (IOException ex) {
	       // log(ex);
		 ex.printStackTrace();
	 }
      
  }
}

-------------------------------------------------
Consumer -----------------------------

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;

public class BaseConsumer {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
    QueueingConsumer consumer = new QueueingConsumer(channel);
   
    channel.basicConsume(QUEUE_NAME, true, consumer);
    
    Object fileObj = null;
    while (true) {
      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
      //String message = new String(delivery.getBody());
      FileOutputStream out =null;
      try {  
      	out = new
FileOutputStream("C:\\Folder\\Saurabh\\BSM\\video\\test-output.docx");
      	
      	ObjectOutputStream save = new ObjectOutputStream(out);
      	save.writeObject(delivery.getBody());
      	save.close();

          out.close();
      } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
      
      System.out.println(" [x] Received '" );
    }
  }
}



--
View this message in context: http://rabbitmq.1065348.n5.nabble.com/binary-data-corrupts-when-transferring-on-Rabbitmq-tp24062.html
Sent from the RabbitMQ mailing list archive at Nabble.com.


More information about the rabbitmq-discuss mailing list