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); }
}
source share