这是我的第一篇文章。我只是更新来写我做了什么
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()));
答案 0 :(得分:0)
使用
构造绝对路径File file = new File(ClientDirectory + fileName);
但是在构造流时你仍然使用文件名:
BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileName));
用
替换此行BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
它应该会更好。
那就是说,你应该阅读Java IO tutorial,因为你没有正确使用文件和流。例如,您没有读取文件的所有字节,也没有关闭finally块中的流。