Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取文件的javajsch_Java_Ssh - Fatal编程技术网

获取文件的javajsch

获取文件的javajsch,java,ssh,Java,Ssh,我在Windows上有一个Java Web服务应用程序,现在需要一个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" 下面的主题与此类似,但我不能使用“StrictHostKey

我在Windows上有一个Java Web服务应用程序,现在需要一个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”部分,因为我需要维护文件的安全性

解决方案似乎指向将hosts文件添加到.ssh,但所有信息都是针对Linux\Unix的。有人能告诉我如何在Windows上执行此操作吗

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


谢谢

请发布您的代码。我想:同样的方法。可能需要一个.bat在用户目录中创建开始期间的目录
.ssh
(或者
ssh
是否足够?)。不确定。我已经按照Maxim的要求添加了功能代码。谢谢
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;
    }