drizzom
drizzom

Reputation: 37

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();

Upvotes: 0

Views: 269

Answers (1)

maerics
maerics

Reputation: 156384

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.

Upvotes: 2

Related Questions