[英]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 ).
既然Sender得到了一个参数,那么我想如果我给出一个像/home/user/test.txt这样的参数,那就没问题了,它会很好但我不知道如何将文件存储到特定文件夹(换句话说,服务器部分)。 Any ideas?
有任何想法吗?
If I'm wrong about the argument please by all means correct me :D 如果我对这个论点不对,请务必纠正我:D
PS: It works just fine for the Netbeans' default folder ( no specification of folder for the Sender or Server ). PS:它适用于Netbeans的默认文件夹(没有发件人或服务器的文件夹规范)。
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(). 在下面的代码(FileReciever)中,我添加了一个新的变量文件夹,该文件夹是从传递给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);
我改变的唯一其他行是: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);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.