How to send a file..?

Haiya...

I'm trying to send a txt file within an Ambient system using TCP.

Is there any java.net API methods which I can use to transfer

the whole file to the other end? I can't use DatagramSocket class's

send () because it requires bytes of data...

I can probably use like... <i copied it from the tuts>

byte[] buf = new byte[256];

DatagramPacket packet = new DatagramPacket(buf, buf.length);

socket.receive(packet);

But, my data is String, and the DatagramPacket constrcutor paramters

are Byte type. The data is already stored as txt file in the client computer.

I'm looking for a way to transfer that file to the server system.

Any existing API methods..? Appreciated, very much.

Jey

[791 byte] By [Java_Jey] at [2007-9-26 4:27:36]
# 1

these following functions may help u...

pacific

DatagramPacket recvPacket () throws IOException

{

byte [] bytes = new byte [chatServer.PACKET_SIZE];

DatagramPacket clientPacket = new DatagramPacket (bytes, bytes.length);

threadSocket.receive (clientPacket);

return clientPacket;

}

void sendPacket (String clientMessage) throws IOException

{

byte [] bytes = clientMessage.getBytes ();

DatagramPacket packetToSend = new DatagramPacket (bytes,bytes.length,clientAddress,clientPort);

threadSocket.send (packetToSend);

}

String extractMessage (DatagramPacket clientPacket)

{

byte [] clientData = clientPacket.getData ();

String clientMessage = (new String (clientData)).trim ();

return clientMessage;

}

i_pacific at 2007-6-29 17:37:28 > top of Java-index,Archived Forums,Socket Programming...
# 2
Try reading the file into an array of bytes and then using DataOutputStream send the array. I works for me. At the other end you have to read the bytes and write them to a file with the same name.
cmitulescu at 2007-6-29 17:37:28 > top of Java-index,Archived Forums,Socket Programming...
# 3

hi,

So, how're files normally transferred in Java lang.? The data is transferred

into stream of arrays via TCP and then the bytes of data is assembled back

at the destination to create a file?

If you know, would you mind giving me some website regarding datagrams,

file transfers techniques regarding in Java?

Thanks for replying my early mail.

Jey

Java_Jey at 2007-6-29 17:37:28 > top of Java-index,Archived Forums,Socket Programming...
# 4

Hi Pacific,

Thanks a lot for your code. I haven't fully checked the code you gave

yet...it looks like it can only transfer a String data. Is there any other

built in java lib. methods I can use which can transfer a binary file

to another computer.

Thanks very much.

Jey

Java_Jey at 2007-6-29 17:37:28 > top of Java-index,Archived Forums,Socket Programming...
# 5
I am not sure why you have to use Datagram or why you can not send bytes?
cmitulescu at 2007-6-29 17:37:28 > top of Java-index,Archived Forums,Socket Programming...
# 6

hi,

yeah, the code works only for the string data transfer. to my knowledge there is no library available for transfering the binary file thru datagramsockets.

But java provides enuf libraries using which u can write the similar functions for other type of data. just c various types of IOstreams....

~Pacific

i_pacific at 2007-6-29 17:37:28 > top of Java-index,Archived Forums,Socket Programming...