?
Solved

Client-Server file transfer

Posted on 2006-05-11
7
Medium Priority
?
299 Views
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:

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

while (in.read(buffer,0,512) != -1)
    out.write(buffer);

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

while (in.read(buffer,0,512) != -1)
    out.write(buffer);


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?
0
Comment
Question by:m4trix
  • 3
4 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16664458
>> while (in.read(buffer,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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16664460
Use this for reading the file into the array:

http://javaalmanac.com/egs/java.io/File2ByteArray.html
0
 
LVL 7

Author Comment

by:m4trix
ID: 16664486
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
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 300 total points
ID: 16664604
>> 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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

862 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