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.
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.
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.
Squanch that. And squanch this tiny ad:
professionally read, modify and write PDF files from Java