本示例是从一个节点上读取文件,并经过调用接口将文件传输另外一个节点上,并存储在数据库java
思路是从源服务器上下载时要将文件流存储到一个临时文件里,而后将临时文件读取为base64的转码,将base64的转码发送到目标服务器的接口上就实现了node
我理解,无论是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方法中,我在本机上测试验证是经过的,可是提交到服务器以后验证就不经过,报异常:json
Could not open or read /root/.ssh/known_hosts: ecdsa-sha2-nistp256 is not supportedwindows
没有找到是什么缘由,不是文件的读写权限的问题,我已经改过了,并且在windows上测试的时候是经过的,后来我就直接将isHostFileWriteable注释掉,强行验证经过就能够用了,没有找到根本缘由,还得继续找,先这么用着api