Socket file transfer

I found this code that I tried and it works fine, but (!). I want to save the file in the folder that I select, and also get from the folder that I will select again. Since Sender receives the argument, I believe that if I give an argument, for example /home/user/test.txt, then this is normal and everything will be fine, but I cannot save the file in a specific folder (part of the server in other words). Any ideas?

If I am mistaken in the argument, please be sure to correct me: D

PS: it is great for the default Netbeans folder (without specifying the folder for the sender or server).

Any help was appreciated.

+4
source share
1 answer

Honestly, although I feel bad at doing homework, I'm just in a good mood :)

In the code below (FileReciever), I added a new variable folder, which is initialized from the first argument passed to main (). Thus, the name of the folder you want to save the file in mus tbe is passed as the first argument. The only other line that I changed is: File file = new File (folder, filename);

private String folder = ""; public static void main(String[] args) { try { folder = args[0]; ServerSocket listener = new ServerSocket(port); while (true) { FileReceiver file_rec = new FileReceiver(); file_rec.socket = listener.accept(); new Thread(file_rec).start(); } } catch (java.lang.Exception ex) { ex.printStackTrace(System.out); } 

}

 public void run() { try { InputStream in = socket.getInputStream(); int nof_files = ByteStream.toInt(in); for (int cur_file=0;cur_file < nof_files; cur_file++) { String file_name = ByteStream.toString(in); File file=new File(folder, file_name); ByteStream.toFile(in, file); } } catch (java.lang.Exception ex) { ex.printStackTrace(System.out); } 

}

+5
source

All Articles