Java 将文件从一台服务器复制到另一台服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3812673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:23:14  来源:igfitidea点击:

Copy file from one server to another

java

提问by Raj

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

我需要将一个文本文件从一台服务器复制到另一台服务器(两台服务器都是 Linux)。我如何在 Java 中做到这一点?

回答by Andreas Dolk

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

如果您能够使用apache commons-io 则简单FileUtils该类具有复制文件的便捷方法:

FileUtils.copyFileToDirectory(srcFile, targetDirectory);

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

(当你谈到我假设的文本文件时,你的应用程序可以访问两个文件系统)

回答by Sean Patrick Floyd

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

如果您需要从可访问的文件系统复制文件,请使用Andreas 的回答

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:

如果您想要一种从底层协议中抽象出来的通用方法,请查看Apache Commons VFS。它为通过多种协议可用的资源提供了一个通用的 api :

  • FTP
  • Local Files
  • HTTP and HTTPS
  • SFTP
  • Temporary Files
  • Zip, Jar and Tar (uncompressed, tgz or tbz2)
  • gzip and bzip2
  • res
  • ram
  • mime
  • FTP
  • 本地文件
  • HTTP 和 HTTPS
  • SFTP
  • 临时文件
  • Zip、Jar 和 Tar(未压缩、tgz 或 tbz2)
  • gzip 和 bzip2
  • 资源
  • 内存
  • 哑剧

回答by truly_not_a_bot

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

几乎所有的 linux 机器都会默认运行 SSH 服务器。因此,SCP 是在它们之间复制文件的最佳选择。

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

使用像 JSCH 这样的 SSH 库来做到这一点。您将在此处找到使用 JSCH 进行 SCP 传输的教程。

回答by Sheng Chien

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

如果您使用 FTP,则可以使用 Apache commons/net 中的 FTPClient。

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

回答by Rob

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

如果您想要一种简单的方法并且您的服务器支持 PHP,我推荐使用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.

只需将脚本上传到您要将文件复制到的目录,输入您要复制的文件的 URL,然后单击Transfer。它在不到 2 分钟的时间内复制了一个 1.4GB 的文件,为我节省了大量时间和带宽。

回答by 2787184

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

我已经使用 commons net FTP 将文件从一台服务器传输到另一台服务器。

Maven Dependency :

Maven 依赖:

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

    }