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();
3
  • 6
    What exactly is the problem? Commented Aug 19, 2011 at 20:08
  • 1
    @drizzom: please post the code related to your problem with trying to send a string or integer. We're interested in what doesn't work, not what works =) Commented Aug 19, 2011 at 20:17
  • The problem is that I want to make this send Srting or text, and I have no idea, could you you give me any advise or idea. Commented Aug 19, 2011 at 20:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

And if I want to send a string array do you have any idea? And thanks again.
@drizzom: better to start a new question about how to write an array of strings/integers rather than try to lump it on here.
i post the question here is the link stackoverflow.com/questions/7127227/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.