Search code examples
javastringfile-uploadjfilechooserfile-sharing

filesharing from 2 computers.


Well, I have this program, the download option is working fine. I can access the other computer and download the file from it to my desktop, but the upload is a problem. When I choose the file, the program tries to get the file from the other computer and not mine so the path is wrong. Is there somehow I can use JFileChooser (as I do in download) and put in my computer name or IP address? Before I started to try the access to another computer I did make it work with JFilChooser, but the path messes it up now. Some tips or tricks?

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

Solution

  • If I understood well, you want to use on your upload method this code:

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

    Is it right for you?