Client-Server file transfer

Posted on 2006-05-11
Last Modified: 2010-03-31
What is the best way to transfer files from a client to a server?  I am not sure whether it is best to just use FTP via Java, or if it is easier to write my own file transfer code.  The files that will be transferred are mostly mp3s and txt files, and possibly zip files, so they might range from a few bytes to about 10 mb, and may be either text files or binary files.

I tried writing my own client/server programs, and they work - but the file received by the server is not the same size as on the client, so it is slightly corrupt.

I'm new to networking so I'm not sure the best way to do these things.
Here is the approach I made to just write a basic client/server transfer app.  I have all the socket connection stuff working fine so I'll only show the file transfer bit:

OutputStream out = serverSocket.getOutputStream();
FileInputStream in = new FileInputStream(new File("D:\\mp3s\\test.mp3"));
byte[] buffer = new byte[512];

while (,0,512) != -1)

Server, similarly:
InputStream in = clientSocket.getInputStream();
FileOutputStream out = new FileOutputStream(new File("output.mp3"));
byte[] buffer = new byte[512];

while (,0,512) != -1)

clearly this is simplisitic, but I'm also clearly missing something.
How can I get this to transfer files without data being corrupted? and would it be better to just use FTP?
Question by:m4trix
    LVL 30

    Expert Comment

    >> while (,0,512) != -1)

    You will not always be reading 512 bytes from the file because when its the last chunk that you are reading, you might be reading lesser than 512 bytes. Same way, while writing, you might not want to write the entire array when its the last chunk.
    LVL 30

    Expert Comment

    Use this for reading the file into the array:
    LVL 7

    Author Comment

    definately right about the 512 bytes - good point.  regardless, the file corruption is not just at the very end, but also in the middle as well. (I used a program to compare the file before and after transferring it, and I can see where the second differs from the first) - so even if I fix the 512 byte chunks, the file will still be corrupt in the middle...

    as for the file to byte array, is that the most efficient way to do it? isn't there a lot of overhead having to read the entire file into memory before sending it?  And how would you suggest actually sending it.  Do you think you could give me an example of a good send and receive loop for the client and server?

    Thanks for the response
    LVL 30

    Accepted Solution

    >> isn't there a lot of overhead having to read the entire file into memory before sending it?  

    You can create a byte-array of 512 or 1024 bytes. Read 512 bytes or as many as you want, and then send them to the server and flush the output-stream. Then you can read into the same byte-array again, and send them.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This video teaches viewers about errors in exception handling.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now