我有這樣的文件共享程序,在那裏我可以從本地位置得到MYE JFileChooser的文件選擇器=新的JFileChooser(「C://用戶」),但我想使用IP地址從服務器獲取文件。我有嘗試String hostname =「192.168.1.1」;但它不起作用。當我打開文件選擇器時,我進入我自己的文件夾。一些技巧?連接到一個IP地址,一個真正的文件共享程序
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程序在整個網絡中訪問_shared_文件? –
是的,我想要一個位於PC上的文件夾,我可以使用這臺計算機的IP地址從不同的計算機訪問 –
我沒有一個完整的答案(因此評論),但請參閱[this](http:///stackoverflow.com/q/2420657/1101070)這樣的問題,並試試。 –