S
S
SpeakLive912018-11-16 19:03:07
Java
SpeakLive91, 2018-11-16 19:03:07

How to upload/rename a folder/file to an FTP server using Java tools?

Hello. I found a code on the Internet for uploading files using Java to an FTP server. The catch is that there are no examples for renaming folders and files. How can I do that?
The code below is purely focused on uploading files to an FTP server.

public static void ftpConn(String filePath, String uploadPath) {
    
    String ftpUrl = "ftp://%s:%[email protected]%s/%s;type=i";
    String host = "ip";
    String user = "login";
    String pass = "pass";
    
    ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);

    try {
        URL url = new URL(ftpUrl);
        URLConnection conn = url.openConnection();
        OutputStream outputStream = conn.getOutputStream();
        FileInputStream inputStream = new FileInputStream(filePath);

        byte[] buffer = new byte[2048];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        inputStream.close();
        outputStream.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
    
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question