I'm reading a file from a Google glass device to a PC through a java socket. The rest of my code is being ignored due to an eof exception thrown by a catch statement. An ideas how I can fix this?
Code: to receive an image (and put it in a simple jfame)
ObjectInputStream inFromServer = new ObjectInputStream(
clientimage.getInputStream());
System.out.println("infrom: " + inFromServer.readObject() + "\n");
System.out.println("infrom bytes: " + inFromServer.readByte() + "\n");
System.out.println("infrom something: " + inFromServer.readUTF());
File temp = (File) inFromServer.readObject();
BufferedImage image = ImageIO.read(temp);
System.out.println("image height: " + image.getHeight());
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
The client is taking a picture and sending the file to the PC:
Socket pic_socket = new Socket(ip, 50505);
ObjectOutputStream imageToServer = new ObjectOutputStream(pic_socket.getOutputStream());
imageToServer.writeObject(pictureToSend.getAbsoluteFile());
imageToServer.close();
My output is:
Connection starting ..
image srv connected to: /192.168.1.104
infrom: \storage\emulated\0\DCIM\Camera\20151209_110536_982.jpg
The output you've shown indicates that this line is working:
System.out.println("infrom: " + inFromServer.readObject() + "\n");
And this line is throwing an exception:
System.out.println("infrom bytes: " + inFromServer.readByte() + "\n");
Which makes sense, because you're only writing a single object, and then closing the stream. Your code should be:
File temp;
try (ObjectInputStream inFromServer =
new ObjectInputStream(clientimage.getInputStream()) {
temp = (File) inFromService.readObject();
}
System.out.println("infrom: " + temp + "\n");
... and then get rid of the calls to readByte()
and readUTF()
. They correspond to data that hasn't been written. Note that with the above code, you're also only calling readObject()
once, which is crucial as only a single object has been written.
See more on this question at Stackoverflow