简体   繁体   中英

IBM XMS .Net how do I receive images files like png and jpg from the MQ

I have a .net XMS client to receive messages from the MQ. It can receive text files fine. But the problem starts when I try to receive .zip or png files. The problem is that the file saved is corrupted and always intreprets it as textmessage.

            var filename = "test.png";
            else if (message is IBytesMessage)
            {
               IBytesMessage bytesMessage = (IBytesMessage)message;
               var messageLength = bytesMessage.ReadInt();
               byte[] uploadPayload = new byte[messageLength];
               bytesMessage.ReadBytes(uploadPayload, messageLength);

               var filePath = _fileUtil.SaveBytesFile(fileName, uploadPayload);
               return filePath;
            }
            if (message is ITextMessage)
            {
                var msg = (ITextMessage)message;
                var result = msg.Text;
                var plainTextBytes = Encoding.UTF8.GetBytes(result);
                var filePath = _fileUtil.SaveBytesFile(fileName, plainTextBytes);
                return filePath;

            }

The msg.Text removes the characerters.
I opened the original png file in notepad++ and find the characters, for example below


O-humMkkVøgÆUf¯éÙô

I tried also copying the text and pasting the text in an new file corrupts the file. Am I missing something... gets translated to


O-humMkkV?g?Uf????

Anyway to read it with the missing characters

Send binary data (files) to MQ as type IBytesMessage .

You should use BodyLength to define the size of the byte[], not ReadInt .

Change the following line:

var messageLength = bytesMessage.ReadInt();

To:

var messageLength = (int)bytesMessage.BodyLength;

The ITextMessage logic works as expected when the content is text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM