Search code examples
javaip-addressfile-sharing

connect to an IP address true a file sharing program


I have this file sharing program, where i can get mye files from a local location JFileChooser chooser = new JFileChooser("C://Users"), but i want to get files from a Server using an IP address. I have trying String hostname = "192.168.1.1"; but its not working. When i open the file chooser i get to my own folder. some tips?

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

Thanks in Advance :)


Solution

  • You're using "//" + hostname + "/C://" as the path for your JFileChooser. That's not a valid path. If you're trying to access files in a shared folder on a LAN, the path for that looks like \\hostname\sharename.

    Even if no shared folders have been defined on the remote machine, may be an "administrative share" of the C: drive called C$, so you could use \\hostname\C$. But you have to authenticate as a valid user on that system to have permission to access the share. (I'm not sure how that'll work when you try to acccess the path from a Java program — Windows might pop up a login box for the remote system, or it might just fail.)