English 中文(简体)
Transfer file over socket
原标题:

I found this code witch I tried and it works great but(!). I want to store the file in a folder that I will choose and also get it from a folder that I again will chose. Since the Sender get an argument then I suppose that if I give an argument like /home/user/test.txt then that s ok and it ll work out fine but I don t get how to store the file to a specific folder ( the Server part in other words ). Any ideas?

If I m wrong about the argument please by all means correct me :D

PS: It works just fine for the Netbeans default folder ( no specification of folder for the Sender or Server ).

Any help appreciated.

最佳回答

Frankly speaking, though i feel bad about doing your homework, I am just in a good mood :)

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

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

}

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签