Sending integer or string from java server
I have a java server, that can send any kind of file, but I can't manage to send a string or a integer.
Here is an example of how the file is sent.
开发者_如何学编程File f = new File("/Users/Large/Downloads/android.jpg");
FileInputStream fis = null;
int size = (int)f.length();
byte[] bytes = new byte[size];
fis = new FileInputStream( f );
fis.read( bytes );
System.out.println("entro...");
out.write( bytes );
out.flush();
Try replacing your call to out.write(bytes)
with these examples that use the String and ByteBuffer classes.:
// A string ("Hello, World").
out.write("Hello, World".getBytes());
// An integer (123).
out.write(ByteBuffer.allocate(4).putInt(123).array());
Note that at some point you will need to care about endianness and character encoding to have something at the other end read these values in a reliable way.
精彩评论