Java中将本服务器的文件拷贝到另一个服务器(Linux to Linux)

在Java中,将文件从一个服务器复制到另一个服务器,你可以使用Secure Copy(SCP)进行操作。Java中的JSch库可以进行此操作。

首先,需要添加JSch库依赖到你的项目中。如果你使用的是Maven,可以添加以下依赖:

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>

以下是一个使用JSch进行SCP操作的示例:

import com.jcraft.jsch.*;

public class Main {
    public static void main(String[] args) {
        String user = "username";
        String host = "hostname";
        String rfile = "/path/to/remote/file.txt";
        String lfile = "/path/to/local/file.txt";

        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, 22);
        UserInfo ui = new MyUserInfo("password");
        session.setUserInfo(ui);
        session.connect();

        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand("scp -t " + rfile);

        OutputStream out = channel.getOutputStream();
        InputStream in = channel.getInputStream();

        channel.connect();

        byte[] buf = new byte[1024];
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();

        // 发送文件
        File file = new File(lfile);
        long filesize = file.length();
        String command = "C0644 " + filesize + " ";
        if (lfile.lastIndexOf('/') > 0){
            command += lfile.substring(lfile.lastIndexOf('/')+1);
        } else {
            command += lfile;
        }
        command += "\n";
        out.write(command.getBytes());
        out.flush();

        // 以流的形式进行读写
        FileInputStream fis = new FileInputStream(file);
        while (true) {
            int len = fis.read(buf, 0, buf.length);
            if (len <= 0) break;
            out.write(buf, 0, len);
        }
        fis.close();
        
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();

        out.close();
        channel.disconnect();
        session.disconnect();
    }

    public static class MyUserInfo implements UserInfo {
        private String password;

        public MyUserInfo(String password) {
            this.password = password;
        }

        @Override
        public String getPassphrase() {
            return null;
        }

        @Override
        public String getPassword() {
            return password;
        }

        @Override
        public boolean promptPassword(String s) {
            return true;
        }

        @Override
        public boolean promptPassphrase(String s) {
            return false;
        }

        @Override
        public boolean promptYesNo(String s) {
            return true;
        }

        @Override
        public void showMessage(String s) {
            System.out.println(s);
        }
    }
}

这段代码首先创建了一个用于SCP的连接,然后按照SCP的通信格式,发送了文件信息和文件内容。其中MyUserInfo类用于处理连接时的身份验证。

你的要求中提到“附带中文注释”,这个需求可能需要你进一步明确。如果你指的是在源代码中添加中文注释,那么你可以在上面的代码中随意添加。如果你需要在传输的文件中添加注释,那么你需要先修改本地文件,然然后再进行文件的传输。这将取决于文件的类型和格式,如果是文本文件,你可以直接在文件中添加注释。

请注意,在使用上述代码时,需要将usernamehostname/path/to/remote/file.txt/path/to/local/file.txt替换为你的实际情况。同时,MyUserInfo类中的password应替换为实际的用户密码。

另外,该段代码假设你的服务器使用的是22端口进行SSH连接,如果你的服务器使用的端口不是22,需要将代码中的22替换为你的实际端口。同时,代码中的"scp -t " + rfile是SCP的标准命令,用于将文件复制到服务器的指定路径,rfile代表远程服务器上的文件路径。

这段代码没有处理所有可能出现的异常,你可能需要根据需要添加适当的异常处理代码。同时,由于网络传输可能会较慢,你可能需要在实际应用中添加超时处理的逻辑。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会理财的程序员不是好摄影师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值