java 从linux下载文件 并传输到另一个节点

本示例是从一个节点上读取文件,并通过调用接口将文件传输另一个节点上,并存储在数据库

思路是从源服务器上下载时要将文件流存储到一个临时文件里,然后将临时文件读取为base64的转码,将base64的转码发送到目标服务器的接口上就实现了

我理解,不管是java还是js里,图片和文档都是转为base64的转码,然后通过rest方式来传输的,但如果文件比较大了,那应该不能这么做了

public static void main(String[] args) {
    JSONObject transferpdf = File2Json.getConfig("transferpdf"); //这个是读取配置文件的
    JSONArray nodes = transferpdf.getJSONArray("node");
    nodes.forEach(item -> {
        JSONObject jsonObject = JSONObject.parseObject(item.toString());
        String ip = jsonObject.getString("ip");
        String password = jsonObject.getString("password");
        String username = jsonObject.getString("username");
        String dir = jsonObject.getString("dir");
        SshClient client = new SshClient();
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            String hosts = "/root/.ssh/known_hosts";
            ConsoleKnownHostsKeyVerification console = new ConsoleKnownHostsKeyVerification(hosts);
            client.connect(ip, 22, console);//IP和端口
            //设置用户名和密码
            PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
            pwd.setUsername(username);
            pwd.setPassword(password);
            int result = client.authenticate(pwd);
            String reg = "[A-Za-z0-9]{10,}_[0-9]{10,}_[0-9]{8,}.pdf";
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = null;
            if (result == AuthenticationProtocolState.COMPLETE) {//如果连接完成
                SftpClient sftp = client.openSftpClient();
                List<SftpFile> list = sftp.ls(dir);
                for (SftpFile f : list) {
                    String name = f.getFilename();
                    matcher = pattern.matcher(name);
                    if (matcher.matches()) {
                        String path = f.getAbsolutePath();
                        System.out.println(path);
                        String filename = name.split("\\.")[0];
                        String fpqqlsh = filename.split("_")[0];
                        File file = File.createTempFile(filename, ".pdf"); //创建临时文件
                        fos = new FileOutputStream(file);
                        bos = new BufferedOutputStream(fos);
                        FileAttributes fa = sftp.get(f.getAbsolutePath(), bos);
                        bos.write(fa.toByteArray());
                        String pdf = PDFToBase64(file);
                        file.deleteOnExit();
                        JSONObject reqJson = new JSONObject();
                        reqJson.put("filename", filename);
                        reqJson.put("pdf", pdf);
                        reqJson.put("fpqqlsh", "7E275D9B64FB43BCB56");
                        InvoiceAPI invoiceAPI = InvoiceAPI.getInstance();
                        String url = "http://192.168.13.52:33890/einvoice/apis/v1.0/uploadInvoice";
                        JSONObject respJson = invoiceAPI.executeRequest(reqJson.toJSONString(), url);
                        System.out.println(respJson);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            client.disconnect();
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                }
            }

        }
    });

}

 

public static String PDFToBase64(File file) {
        BASE64Encoder encoder = new BASE64Encoder();
        FileInputStream fin = null;
        BufferedInputStream bin = null;
        ByteArrayOutputStream baos = null;
        BufferedOutputStream bout = null;
        try {
            fin = new FileInputStream(file);
            bin = new BufferedInputStream(fin);
            baos = new ByteArrayOutputStream();
            bout = new BufferedOutputStream(baos);
            byte[] buffer = new byte[1024];
            int len = bin.read(buffer);
            while (len != -1) {
                bout.write(buffer, 0, len);
                len = bin.read(buffer);
            }
            //刷新此输出流并强制写出所有缓冲的输出字节
            bout.flush();
            byte[] bytes = baos.toByteArray();
            String base64 = encoder.encodeBuffer(bytes).trim();
//            System.out.println(base64);
            return base64;

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fin.close();
                bin.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.transport.AbstractKnownHostsKeyVerification;
import com.sshtools.j2ssh.transport.InvalidHostFileException;
import com.sshtools.j2ssh.transport.publickey.SshPublicKey;

import java.io.*;
import java.util.List;

/**
 * Created by garila on 2017/6/28.
 */
public class ConsoleKnownHostsKeyVerification extends AbstractKnownHostsKeyVerification {

    public ConsoleKnownHostsKeyVerification() throws InvalidHostFileException {
        super(new File(System.getProperty("user.home"), ".ssh" + File.separator + "known_hosts").getAbsolutePath());
    }

    public ConsoleKnownHostsKeyVerification(String knownhosts) throws InvalidHostFileException {
        super(knownhosts);
    }

    public void onHostKeyMismatch(String host, SshPublicKey pk, SshPublicKey actual) {
        try {
            System.out.println("The host key supplied by " + host + " is: "
                    + actual.getFingerprint());
            System.out.println("The current allowed key for " + host + " is: "
                    + pk.getFingerprint());
            getResponse(host, pk);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onUnknownHost(String host, SshPublicKey pk) {
        try {
            System.out.println("The host " + host
                    + " is currently unknown to the system");
            System.out.println("The host key fingerprint is: "
                    + pk.getFingerprint());
            getResponse(host, pk);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取用户输入的信息,判断是否接受主机公匙
     * <p>
     * 修改:xxx ,去掉从流中获取信息,直接接受公匙,注释掉的代码为源码
     *
     * @param host 主机ip
     * @param pk   主机公匙
     * @throws InvalidHostFileException
     * @throws IOException
     */
    private void getResponse(String host, SshPublicKey pk) throws InvalidHostFileException, IOException {
//        if (isHostFileWriteable()) {
//        }
        allowHost(host, pk, true);
    }
}

在getResponse方法中,我在本机上测试验证是通过的,但是提交到服务器之后验证就不通过,报异常:

Could not open or read /root/.ssh/known_hosts: ecdsa-sha2-nistp256 is not supported

没有找到是什么原因,不是文件的读写权限的问题,我已经改过了,而且在windows上测试的时候是通过的,后来我就直接将isHostFileWriteable注释掉,强行验证通过就可以用了,没有找到根本原因,还得继续找,先这么用着

转载于:https://my.oschina.net/u/2499632/blog/1120346

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值