Copy file from one server to another

I need to copy a text file from one server to another (both servers are Linux). How to do it in Java?

+5
source share
6 answers

It is easy if you can use apache commons-io : the FileUtilsclass has convenient methods for copying files:

FileUtils.copyFileToDirectory(srcFile, targetDirectory);

(as you said about text files, I assume that your application has access to both file systems)

+4
source

If you need to copy files from available file systems, open Andreas answer .

, , Apache Commons VFS. , :

  • FTP
  • HTTP HTTPS
  • SFTP
  • Zip, Jar Tar (, tgz tbz2)
  • gzip bzip2
+3

FTP, FTPClient Apache/net.

:


FTPClient client = new FTPClient();
client.connect(host);

if(FTPReply.isPositiveCompletion(client.getReplyCode())) {
  if(client.login(username, password)) {
    FileInputStream fis = new FileInputStream(localFilepath);

    try {
      if(client.storeFile(remoteFilename, fis)) {
        System.out.println("File uploaded!");
      }
    }
    finally {
      fis.close();
    }  
  }
}
+1

Linux- SSH-. , SCP .

SSH, ​​ JSCH. SCP H .

0

If you need an easy way and your server supports PHP, I recommend Rapid Transfer Script .

Just upload the script to the directory where you want to copy the file, enter the URL of the file you want to copy, and click "Transfer." It copied a 1.4 gigabyte file in less than 2 minutes and saved me a lot of time and bandwidth.

0
source

I used commons net FTP to transfer a file from one server to another.

Maven dependency:

    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>3.3</version>
    </dependency>

Java:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;


    public void tranferFile() {

        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(servername, port);
            ftpClient.login(username, password);
            ftpClient.enterLocalPassiveMode();

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            File sourceFile = new File("file which you want to send");
            InputStream inputStream = new FileInputStream(sourceFile);

            boolean done = ftpClient.storeFile("filename which receiver get", inputStream);
            inputStream.close();
            if (done) {
                LOGGER.info("file is uploaded successfully..............");
            }

        } catch (IOException e) {
            LOGGER.error("Exception occured while ftp : "+e);
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException e) {
                LOGGER.error("Exception occured while ftp logout/disconnect : "+e);
            }
        }

    }
0
source

All Articles