Hi!

I would like to develop a software that will allow me to find a file (using JFileChooser) on a local machine and save it to a remote machine. I know an IP address of the remote machine. Please, give me some explanations or a link to some tutorials regarding this question, because I have no idea how to start implementing it.

Thanks a lot!

Recommended Answers

All 4 Replies

Ive encountered this problem before. Just forgot where I found the code.

Try to Google 'java FTP' or 'apache ftp'.

Thank you! I installed FileZilla Server and everything works fine. However, there is just one little problem. When I copy the file from my local machine to the remote machine, then the file name is set as "null" (the content of the file is correct). Why does it happen?

private void saveAttachedFileToRemoteMachine() {
            FTPClient ftp = new FTPClient();
            ftp.setHost(url);
            ftp.setUser(login);
            ftp.setPassword(pass);
            boolean connected = ftp.connect();
            
            ftp.setRemoteFile(text4.getText());
            if ( connected){
              if (ftp.uploadFile(pathToFile))
                System.out.println(ftp.getLastSuccessMessage ());
              else
                System.out.println(ftp.getLastErrorMessage ());
              }
              else
                System.out.println(ftp.getLastErrorMessage ());
    }

This is the uploadFile function that I found in internet:

public synchronized boolean uploadFile (String localfilename)
  {
    try{

      InputStream is = new FileInputStream(localfilename);
      BufferedInputStream bis = new BufferedInputStream(is);
      OutputStream os = m_client.getOutputStream();
      BufferedOutputStream bos = new BufferedOutputStream(os);
      byte[] buffer = new byte[1024];
      int readCount;

      while( (readCount = bis.read(buffer)) > 0)
      {
            bos.write(buffer, 0, readCount);
      }
      bos.close();

      this.succMesg = "Uploaded!";

      return true;
    }
    catch(Exception ex)
    {
      StringWriter sw0= new StringWriter ();
      PrintWriter p0= new PrintWriter ( sw0, true );
      ex.printStackTrace ( p0 );
      erMesg = sw0.getBuffer().toString ();

      return false;
    }
  }

Ok, I solved this problem using Apache Commons library. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.