Link to home
Start Free TrialLog in
Avatar of linchong
linchong

asked on

big file upload to server with resume function

Hi

I am trying to write a file upload program to a server through sockets.
as thefile may be very big, i need a resume function in case connection breaks and i can pick up the upload from where it was broken.

Is there any example or source codes out there?

Hope it is not too complicated
Avatar of sudhakar_koundinya
sudhakar_koundinya

If you are not using some library that provides the communication protocol (such as ftp) then you
can have as part of your communication protocol some negotiation about the start from byte value.
Then you can use in the client side RandomAccessFile http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html
to perform seek to that position and then use its read method to start reading from it.

randomFile.seek(position);
byte bytes[] = new byte[1024];
int length = 0;
while ((length = randomFile.read(bytes)) > 0)
   out.write(bytes, 0, lenght);
Avatar of linchong

ASKER

which means if the connection breaks, i check the length of uploaded file currently in server. get this position.

when client try to resume upload,  send back the last positon to client to start from that position and resume upload??

Avatar of CEHJ
You need both sides cooperating to do this or the partial file on the server probably will just get clobbered by the resumed upload. Are you implementing this yourself? Why not do it with FTP? This has the APPE command that appends to a file
but FTP upload has not resume function. only download has it.

where can i fidn the example??
>> but FTP upload has not resume function

That's not the case. The APPE command does just that effectively (it appends to a file). You need a library that supports that
where can i find an example on this??
also, there is no ftp server over the other end

it is just an url socket server ..  I am taking inputstream to bytearrays and then to fileoutput
>> also, there is no ftp server over the other end

Then this discussion of ftp is somewhat academic unless you use one. You'll need to develop a custom protocol whereby the receiving file can be appended to
what i have in mind is to name this file with ref to userid n filename.
whatever recvd thru socket will be appended to this file.

i have never try RandomAccessFile yet. not sure what is the maximum file size available. And whether it would fit into this scenario.

any better ideas?
There's no need to use a RAF to append to a file. Just open the output stream on it in append mode
if connection breaks, it is a new session .......
i need to know where it had stop,  pick up fro that point and resume
can someone explain to me how ftp resume works??  I need an idea how it works
>>i need to know where it had stop,  pick up fro that point and resume

You need a strategy for communication between both ends so you know the state

>> can someone explain to me how ftp resume works??  I need an idea how it works

Resume works from the downloading standpoint - you're uploading
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial