0

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();
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
drizzom
  • 37
  • 1
  • 7

1 Answers1

2

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.

maerics
  • 151,642
  • 46
  • 269
  • 291
  • And if I want to send a string array do you have any idea? And thanks again. – drizzom Aug 19 '11 at 20:39
  • @drizzom: better to start a new question about how to write an array of strings/integers rather than try to lump it on here. – maerics Aug 19 '11 at 20:41
  • i post the question here is the link http://stackoverflow.com/questions/7127227/how-to-send-a-string-array-using-sockets-and-objectoutputstream – drizzom Aug 19 '11 at 20:47