这是我的第一篇文章。我只是更新一下来写我所做的
import java.io.*;
import java.rmi.*;
import javax.swing.JOptionPane;
public class FileClient{
public static void main(String a[]) {
Object[] choice = {"download", "upload"};
int valg = JOptionPane.showOptionDialog(null, "What do u want to do?", null, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, "");
switch(choice){
case 0:
try {
String filename = JOptionPane.showInputDialog("what do u want to copy? ");
String name = "rmi://" + "localhost" + "/FileServer";
FileInterface fi = (FileInterface) Naming.lookup(name);
byte[] filedata = fi.downloadFile(ServerDirectory + filnavn);
File file = new File(filnavn);
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file.getName()));
output.write(filedata,0,filedata.length);
output.flush();
output.close();
System.out.println(file.getAbsolutePath());
} catch(Exception e) {
System.err.println("FileServer exception: "+ e.getMessage());
e.printStackTrace();
}
break;
case 1:
break;
}
}
}
我遇到的问题是我无法在代码中选择工作目录。
所以我学到的是你不能在java中选择工作目录文件夹,所以我只输入路径,这样我就可以在哪里获取文件以及我想在哪里下载它们:
String PathToDownloadFolder = ("//C:/Hello/")
String PathToWhereIGetTheFile = ("//C:/server/")
byte[] filedata = cf.downloadFile(PathToWhereIGetTheFile + valgtFil);
BufferedOutputStream output = new BufferedOutputStream
(new FileOutputStream(PathToDownloadFolder + file.getName()));