Raj
Raj

Reputation: 61

Copy file from one server to another

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

Upvotes: 6

Views: 38816

Answers (6)

Andreas Dolk
Andreas Dolk

Reputation: 114767

Easist if you're able to use apache commons-io: the FileUtils class has convenient methods to copy files:

FileUtils.copyFileToDirectory(srcFile, targetDirectory);

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

Upvotes: 4

2787184
2787184

Reputation: 3881

I have used commons net FTP to transfer 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);
            }
        }

    }

Upvotes: 0

Rob
Rob

Reputation: 1

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

Just upload the script to the directory you want to copy the file to, enter the URL of the file you want to copy and click Transfer. It copied a 1.4GB file across in under 2 minutes and saved me a lot of time and bandwidth.

Upvotes: 0

Sheng Chien
Sheng Chien

Reputation: 519

If you go with FTP, you could use the FTPClient from Apache commons/net.

Here are some sample codes for your reference:


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

Upvotes: 1

truly_not_a_bot
truly_not_a_bot

Reputation: 41

Almost all linux machines will be having SSH server running by default. So SCP would be your best bet to copy files between them.

Use a SSH library like JSCH to do this. You will find a tutorial to do SCP transfer using JSCH here.

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

If you need to copy files from accessible file systems go with Andreas' answer.

If you want a general approach that abstracts from the protocol underneath, have a look at Apache Commons VFS. It provides a common api for resources available through a number of protocols:

  • FTP
  • Local Files
  • HTTP and HTTPS
  • SFTP
  • Temporary Files
  • Zip, Jar and Tar (uncompressed, tgz or tbz2)
  • gzip and bzip2
  • res
  • ram
  • mime

Upvotes: 6

Related Questions