来自2台计算机的文件共享。

时间:2012-01-24 12:20:54

标签: java string file-upload jfilechooser file-sharing

好吧,我有这个程序,下载选项工作正常。我可以访问另一台计算机并将文件从它下载到我的桌​​面,但上传是个问题。当我选择文件时,程序会尝试从另一台计算机获取文件而不是我的文件,因此路径错误。有什么方法我可以使用JFileChooser(就像我下载)并输入我的计算机名称或IP地址?在我开始尝试访问另一台计算机之前,我确实让它与JFilChooser一起工作,但这条路现在让它变得混乱。一些提示或技巧?

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();
        }
    }

1 个答案:

答案 0 :(得分: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();
   }
}

适合你吗?