2012-01-24 13 views
0

まあ、私はこのプログラムを持って、ダウンロードオプションは正常に動作しています。他のコンピュータにアクセスしてそのファイルを自分のデスクトップにダウンロードできますが、アップロードは問題です。ファイルを選択すると、プログラムは他のコンピュータからファイルを取得しようとしますが、そのパスは間違っています。何とかJFileChooserを(私がダウンロードしているように)使用して、自分のコンピュータ名またはIPアドレスを入れることができるのですか?別のコンピュータへのアクセスを試みる前に、私はJFilChooserで動作させるようにしましたが、パスは今すぐ上になります。いくつかのヒントやトリックですか?2台のコンピュータからのファイル共有。

public void upload(String username) throws RemoteException, NullPointerException{ 

try { 
      System.out.println(getProperty); 
      String lol = "/hei/hei.txt"; 
      String ServerDirectory=("//" + "JOAKIM-PC"+ "/Users/Public/Server/"); 
      byte[] filedata = cf.downloadFile2(getProperty + lol); 
      File file = new File(getProperty + lol); 
      BufferedOutputStream output = new BufferedOutputStream 
        (new FileOutputStream(ServerDirectory + file.getName())); 
      output.write(filedata,0,filedata.length); 
      output.flush(); 
      output.close(); 
     } catch(Exception e) { 
      System.err.println("FileServer exception: "+ e.getMessage()); 
      e.printStackTrace(); 
     } 
    } 
    public void download(String username) throws RemoteException, NullPointerException{       
     JFileChooser chooser = new JFileChooser("//" + "JOAKIM-PC" + "/Users/Joakim/Server/"); 
     chooser.setFileView(new FileView() { 
      @Override 
      public Boolean isTraversable(File f) { 
       return (f.isDirectory() && f.getName().equals("Server")); 
      } 
     }); 
     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 clientDirectory = getProperty + "/desktop/"; 
      byte[] filedata = cf.downloadFile(selectedFile); 
      System.out.println("Byte[] fildata: " + selectedFile); 
      File file = new File(fileName); 
      System.out.println("fileName: " + fileName); 
      BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(clientDirectory + file.getName())); 
      output.write(filedata, 0, filedata.length); 
      output.flush(); 
      output.close(); 
     } catch (Exception e) { 
      System.err.println("FileServer exception: " + e.getMessage()); 
      e.printStackTrace(); 
     } 
    } 
+0

* 'System.out.println(getProperty);' *あなたは 'getProperty'という名前の属性を持っていますか?それを取得するための 'getGetProperty()'とそれを設定する 'setGetProperty()'はありますか?それは*非常に混乱するに違いない。 –

+0

String getProperty = System.getProperty( "user.home"); –

答えて

1

私はよく理解した場合、あなたはあなたのupload方法でこのコードを使用する:

JFileChooser chooser = new JFileChooser("./hei/"); 
int returnval = chooser.showOpenDialog(this); 
String ServerDirectory=("//" + "JOAKIM-PC"+ "/Users/Public/Server/"); 
if(returnval == JFileChooser.APPROVE_OPTION){ 
    File file = chooser.getSelectedFile(); 

    try{ 
      byte[] filedata = cf.downloadFile2(file.getAbsolutePath()); 
      BufferedOutputStream output = new BufferedOutputStream 
        (new FileOutputStream(ServerDirectory + file.getName())); 
      output.write(filedata,0,filedata.length); 
      output.flush(); 
      output.close(); 

    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

は、それは右のあなたのためですか?

+0

はいこれは正しいです:) –

+0

素晴らしい。この回答に正しいとマークすることを検討することがあります。 :-) –

関連する問題