• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

File transfer from Client to Server side

 
Greenhorn
Posts: 4
PHP Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just replaced the line
with in Server.java file and it worked for me.
 
Srivatsa Sri
Greenhorn
Posts: 4
PHP Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
PHP Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic