Please refer the below code.
Here we will receive message simple file creation then writing bytes coming from BytesMessageObject until EOF occurs.
BytesMessage bm = (BytesMessage)topicSubscriber.receiveNoWait(); // receive(1000);
System.out.println("bm-->"+bm);
if(bm !=null){
File file = new File("D:/jms/myfie.txt"); //file created
FileOutputStream fos = new FileOutputStream(file);//create fileoutput stream
BufferedOutputStream outBuf = new BufferedOutputStream(fos);//create bufferoutputstream
int i;
while((i=bm.readInt())!=-1)//read unitil EOF means -1
{
outBuf.write(i);//write it to the file
}
System.out.println("outBuf-->"+outBuf);
System.out.println(file.isFile());
outBuf.close();//close output buffer
fos.close();//close file ouput stream
}
Here the file is opened then
Wrote in BytesMessageObject.
Sent Across the Queue
Sender code :
File f=new File("D:/Import.txt");//get file handle
System.out.println("is File "+f.isFile());
BytesMessage bm = topicSession.createBytesMessage();//create bytes message
InputStream in= new FileInputStream(f);//create input stream with file handle
BufferedInputStream inBuf= new BufferedInputStream(in);//create buffer input stream
int i;
while((i=inBuf.read())!=-1)//read file until EOF(-1) is reached{
bm.writeInt(i);//write in bytesmessage
}
System.out.println("after while");
//adding an eof
bm.writeInt(-1);//add EOF in bytes message
System.out.println("BM = "+bm);
topicPublisher.send(bm);///send the bytes message
System.out.println("sent successfully");
topicConnection.stop();//stop connection
2015-07-23 12:42:19
+0
请不要随便倾倒一些代码。解释你的代码如何实际解决问题中的问题。 –
2015-07-23 13:03:39
+0
@ekad现在回答说明 –
2015-07-24 06:27:58