【问题标题】:connect to an IP address true a file sharing program连接到 IP 地址 true 文件共享程序
【发布时间】: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


【解决方案1】:

您使用"//" + hostname + "/C://" 作为JFileChooser 的路径。这不是一条有效的路径。如果您尝试访问 LAN 上共享文件夹中的文件,则其路径类似于 \\hostname\sharename

即使远程计算机上没有定义共享文件夹,也可能是 C: 驱动器的“管理共享”,称为 C$,因此您可以使用 \\hostname\C$。但是您必须作为该系统上的有效用户进行身份验证才能获得访问共享的权限。 (我不确定当您尝试从 Java 程序访问路径时它会如何工作 - Windows 可能会弹出一个远程系统的登录框,或者它可能会失败。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 2011-11-03
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多