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 :
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.
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.
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