2012-01-23 30 views
0

我有這樣的文件共享程序,在那裏我可以從本地位置得到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(); 
     } 
    } 

感謝提前:)

+0

您是否想要以某種方式使用Java程序在整個網絡中訪問_shared_文件? –

+0

是的,我想要一個位於PC上的文件夾,我可以使用這臺計算機的IP地址從不同的計算機訪問 –

+0

我沒有一個完整的答案(因此評論),但請參閱[this](http:///stackoverflow.com/q/2420657/1101070)這樣的問題,並試試。 –

回答

1

您使用"//" + hostname + "/C://"爲您JFileChooser路徑。這不是一個有效的途徑。如果你正在嘗試訪問文件的共享文件夾在LAN上,對於該路徑看起來像\\hostname\sharename

即使沒有在遠程計算機上定義共享文件夾,也可能是名爲C$的C:驅動器的「管理共享」,因此您可以使用\\hostname\C$。但是,您必須通過該系統上的有效用戶身份驗證才能獲得訪問該共享的權限。 (我不確定當你試圖從Java程序中獲取路徑時它是如何工作的--Windows可能會彈出遠程系統的登錄框,或者它可能只是失敗。)

+0

謝謝:)工作正常 –

相關問題