【发布时间】:2012-01-23 15:37:48
【问题描述】:
我有这个文件共享程序,我可以从本地位置获取我的文件 JFileChooser chooser = new JFileChooser("C://Users"),但我想使用 IP 地址从服务器获取文件。我试过 String hostname = "192.168.1.1";但它不工作。当我打开文件选择器时,我会进入我自己的文件夹。一些提示?
public void download(String username) throws RemoteException, NullPointerException{
JFileChooser chooser = new JFileChooser("//" + hostname + "/C://");
chooser.setFileView(new FileView() {
@Override
public Boolean isTraversable(File f) {
return (f.isDirectory() && f.getName().equals("C://"));
}
});
int returnVal = chooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
} try {
String fileName = chooser.getSelectedFile().getName();
File selectedFile = chooser.getSelectedFile();
//String name = "//" + hostname + "/chatter";
System.out.println(fileName);
//ChatFront cf = (ChatFront) Naming.lookup(name);
String ClientDirectory = getProperty + "/desktop/";
byte[] filedata = cf.downloadFile(selectedFile);
File file = new File(fileName);
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(ClientDirectory + file.getName()));
output.write(filedata, 0, filedata.length);
notifySelf(getUsername(), "You have now downloaded: " + file.getName() + " from the server");
output.flush();
output.close();
} catch (Exception e) {
System.err.println("FileServer exception: " + e.getMessage());
e.printStackTrace();
}
}
在此先感谢 :)
【问题讨论】:
-
您是否希望通过您的 Java 程序以某种方式访问网络上的共享>文件?
-
是的,我想要一个位于 PC 上的文件夹,我可以使用这 1 台计算机的 IP 地址从不同的计算机访问该文件夹
-
我没有完整的答案给你(因此是评论),但请参阅this SO question 并尝试一下。
-
没关系,我在 Wyzard 的帮助下让它工作了:)
标签: java ip-address file-sharing