0

我在 Windows 上有一个 Java Web Service 应用程序,现在需要一个 SFTP 进程。我正在尝试使用 JSch 实现 SFTP 以通过 SSH 连接到文件位置,但出现错误:

"com.jcraft.jsch.JSchException: UnknownHostKey: *my_IP_Address*. RSA key fingerprint is a6:54:a8:f7:00:ed:e5:62:6b:ec:60:29:db:eb:ad:0c"

下面的主题类似,但我不能使用“StrictHostKeyChecking”部分,因为我需要维护文件的安全性!

com.jcraft.jsch.JSchException: UnknownHostKey

该解决方案似乎指向将主机文件添加到 .ssh 但所有信息都适用于 Linux\Unix。有人可以告诉我如何在 Windows 上执行此操作吗?

这是有问题的函数(它涉及 MD5 加密,因此引用了 MD5);

public DocumentByteArrayWithChecksum getRawBytesFromFile(String filename)
            throws IOException, Exception {

        DocumentByteArrayWithChecksum dba = new DocumentByteArrayWithChecksum();
        InputStream in = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MessageDigest md = MessageDigest.getInstance("MD5");

        String SFTPHost = "my_IP_Address";
        int    SFTPPort = 22; // This is the default sftp port
        String SFTPUsername = "my_Username";
        String SFTPPassword = "my_Password";

        Channel channel = null;
        ChannelSftp channelsftp = null;
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession(SFTPUsername,
                                                              SFTPHost,
                                                              SFTPPort);

        try {
            session.setPassword(SFTPPassword);
            session.connect();
            channelsftp = (ChannelSftp)session.openChannel("sftp");
            channelsftp.connect();

            in = channelsftp.get(filename);

            // used to get an MD5 length
            in = new DigestInputStream(in, md);
            logger.debug("The file is " + filename);
            long length = filename.length();
            if (length > Integer.MAX_VALUE) {
                logger.error("File is too large " + length);
                throw new Exception("file exceeds maximum length of "
                        + String.valueOf(Integer.MAX_VALUE));
            }

            byte[] buffer = new byte[(int) length];
            int n = 0;
            while ((n = in.read(buffer)) != -1) {
                out.write(buffer, 0, n);
            } // End of while block

        } catch(JSchException e) {
            System.err
            .println("%%%% Error in JSch process %%%%");
            e.printStackTrace();
        }
        catch(Exception e) {
            System.err
            .println("%%%% Error during getRawBytesFromFile function %%%%");
            e.printStackTrace();
        }
        finally {
            in.close();
            channelsftp.disconnect();
            session.disconnect();
        }// End of try block
        dba.orginalPDFDocumentChecksum = md.digest();
        dba.bytePDFDocument = out.toByteArray();

        return dba;
    }

谢谢

4

0 回答 0