Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem transfering JPG using RMI  RSS feed

 
Nicolás Muñoz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm programming a client-server application, where the client ask the server for a jpg file. The server then transfers the file (if found) to the client. I'm having a strange issue where all of the jpg bytes are transfered but when I go to the folder where the client keeps the file, and I try to open the file, my Image Viewer (I'm using GNOME) says "Could not load image 'nameOfFile.jpg'. Error interpreting JPEG image file (Not a JPEG file: starts with 0x00| 0x00".

This also happened to me doing the same application but using sockets instead of RMI, and happens also when using Windows instead of Ubuntu. I'm not sure what I am doing wrong. I'll be thankful if anyone can help me.
 
Nisha Ganeriwal
Greenhorn
Posts: 8
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the io streams you are using. You may not be writing the file properly.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you transferring the JPG through RMI?

Communication in RMI is as follows:
- the client sends method call request with all arguments in serialized form to the server
- the server executes the method with the given deserialized arguments
- the server sends the return value in serialized form to the client
- the client gets the deserialized return value

As you see, any changes to the arguments will not be seen by the client. For instance, the following method will not do what you expect when used in RMI:
The server modifies the StringBuilder, but this is not the client's StringBuilder - it's a copy. This copy is not synchronized with the client's copy.

So if your transfer method passes a byte[] to the RMI server's method like in InputStream.read and you expect it to be modified - well, it won't.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you verifying that:
all of the jpg bytes are transfered
?

Are you sure that you are always treating the contents as byte[]? As Nisha said, check your use of streams - watch out for treating the bytes as characters, for example by creating a String or using a Writer or Reader.

Due to unicode character conversions, an image will not survive being treated as char[] or String.

Bill

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!