File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes File transfer from Client to Server side Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "File transfer from Client to Server side" Watch "File transfer from Client to Server side" New topic
Author

File transfer from Client to Server side

Srivatsa Sri
Greenhorn

Joined: Aug 28, 2012
Posts: 4

Hello , I am new to coderanch. Please guide me with the following problem. I have Server and Client java files. Client sends a file to Server, Server receives it and saves in the present working directory of Server java file. How to change the target directory in Server from current working directory? Here is my code :

Server.java


Client.java
Daniel Georgescu
Greenhorn

Joined: Apr 30, 2011
Posts: 5

I just replaced the line
with in Server.java file and it worked for me.
Srivatsa Sri
Greenhorn

Joined: Aug 28, 2012
Posts: 4

Hi Daniel,

Thank you for the response. You are right when we upload the one file or the file we know already. But here its dynamic, Client enters the name of the file_path and this file has to be saved in a directory predefined in Server for example :

As of now, the file sent by Client will be saved under the current working directory where Server.java is placed.

Hope you got my point...Verify it. Thanks again.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2415
    
  28

You should do something like this



Here targetDirectory is the full path of folder in which you want to save the file. It will be better if you get the target directory from a configuration item rather than hard coding it. It will make your code more portable.
Srivatsa Sri
Greenhorn

Joined: Aug 28, 2012
Posts: 4

Yes, yeah thats what I need , Jayesh. Thanks for the suggestion. Can you please put it to code ? I tried but, the data is not transferring , only file is transferred with 0KB memory size.

Modified it like this :

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2415
    
  28

Now, you are creating 2 files; one in the same place where you were creating before, one in the place where you want to create. Which FileOutputStream are you writing your bytes to? If you write to fos, and not to location, then the file that location points to will have 0 bytes, and the file that fos points to will have all the bytes

BTW, if you want to create the file in the user's home directory, it's better to use "user.home" system property rather than hardcoding the path in your code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File transfer from Client to Server side