Java JSch获取文件

时间:2013-09-17 14:35:18

标签: java ssh

我在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

解决方案似乎指向将hosts文件添加到.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;
    }

由于

0 个答案:

没有答案