Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Java jsch和中断后恢复文件上载?_Jsch - Fatal编程技术网

Java jsch和中断后恢复文件上载?

Java jsch和中断后恢复文件上载?,jsch,Jsch,我目前正在我的代码中使用com.jcraft.jsch库,这样我就可以将一个或多个文件从本地机器上载到某个远程机器。对于5KB、100KB、200KB的文件大小,我没有任何顾虑。然而,当我倾向于上传一个文件大小为500MB、1GB、2GB及以上的文件时,我有一个很大的担忧,因为在本地计算机或远程计算机上,internet连接总是有可能失败 我自己做了一些研究,发现这个库有一个名为RESUME的字段,它指的是文件传输模式,但我还没有找到关于它正确使用的解释 所以我的问题是:如果连接失败,在修复连接

我目前正在我的代码中使用com.jcraft.jsch库,这样我就可以将一个或多个文件从本地机器上载到某个远程机器。对于5KB、100KB、200KB的文件大小,我没有任何顾虑。然而,当我倾向于上传一个文件大小为500MB、1GB、2GB及以上的文件时,我有一个很大的担忧,因为在本地计算机或远程计算机上,internet连接总是有可能失败 我自己做了一些研究,发现这个库有一个名为RESUME的字段,它指的是文件传输模式,但我还没有找到关于它正确使用的解释


所以我的问题是:如果连接失败,在修复连接后,文件传输会从中断点继续吗

我刚刚在我的应用程序中解决了这个问题,并想与大家分享我学到的东西

我调查了简历的工作情况,发现它取决于你使用的方法。对我来说,我同时使用了PipedInputStream和PipedOutStream,因为我可能会向/从本地文件传输数据,甚至向/从远程服务器传输数据

我发现,对于我来说,我向get方法提供了PipedOutputStream,没有提供覆盖默认值的模式,然后向put方法提供了参数RESUME的PipedInputStream。put方法使我的InputStream前进到与我要发送到的文件的当前大小相等的字节数

这花了一段时间,因为我已经在处理我的PipedOutStream X字节数,然后PipedInputStream又处理了X字节,我得到了很大的间隙。我通过看照片发现了这一点

当然,如果你不做与我完全相同的事情,这将是不同的,但如果你的来源或目的地是本地的,你可能不需要担心这一点。如果你不知道你是怎么做的,我会试着看看源代码

我使用的是Grails,所以这可能不适合您,但下面是我所做的

/*    
 * This was initially copied from
 * <a href="http://www.intelligrape.com/blog/2013/04/04/using-ftp-with-grails/">
 * http://www.intelligrape.com/blog/2013/04/04/using-ftp-with-grails/</a> for
 * the basic structure. JavaDoc and additional method were added as needed.
 *
 * @author Puneet Behl
 * @author jonathan.tinsman
 */
class FtpService {

    /**
     * Gets the file from the server and loads it into the provided output stream
     *
     * @param outputStream
     *      - the output stream to have the file loaded to
     * @param fileName
     *      - the desired file
     * @param ftpCredential
     *      -the server credentials
     */
    def load(OutputStream outputStream, String fileName, FtpCredential ftpCredential) {
        connect(ftpCredential) { ChannelSftp sftp ->
            sftp.get fileName, outputStream, new FtpMonitor()
        }
    }

    /**
     * Writes the file on the server
     *
     * @param inputStream
     *      - the input stream for writing
     * @param fileName
     *      - the file name
     * @param mode
     *      - the mode for the transfer (defaults to {@link ChannelSftp#OVERWRITE}
     * @param ftpCredential
     *      - the server credentials
     */
    def save(InputStream inputStream, String fileName, Integer mode = ChannelSftp.OVERWRITE, FtpCredential ftpCredential) {
        connect(ftpCredential) { ChannelSftp sftp ->
            sftp.put inputStream, fileName, mode
        }
    }

    /**
     * Transfers the file from the input server to the output server.
     * <p>
     * The usage of {@link PipedInputStream} and {@link PipedOutputStream} is
     * from <a href="http://ostermiller.org/convert_java_outputstream_inputstream.html">OsterMiller.org</a>
     *
     * @param fileName
     *      - the file name
     * @param inputFtpCredential
     *      - the input server
     * @param outputFtpCredential
     *      - the output server
     * @param mode
     *      - the mode for the transfer (defaults to {@link ChannelSftp#OVERWRITE}
     */
    def transfer(String fileName, FtpCredential inputFtpCredential, FtpCredential outputFtpCredential, Integer mode = ChannelSftp.OVERWRITE) {

        // To change the size of the buffer, add an int with the desired pipe
        // size. The default is 1024
        PipedInputStream input = new PipedInputStream();
        PipedOutputStream output = new PipedOutputStream(input);

        // Starting in different threads so they do not deadlock each other
        new Thread(
                new Runnable(){
                    public void run(){
                        new FtpService().load output, fileName, inputFtpCredential
                    }
                }
                ).start();
        /* 
         * only passing the mode to the "save" as the save will progress the 
         * input stream on it's own. 
         * 
         * If we pass the mode to the "load" method, then there will be a gap
         * in the data as the "load" will progress the stream xx bytes and the
         * "save" will progress it another xx bytes (the size of the existing
         * file).
         */
        save input, fileName, mode, outputFtpCredential
    }

    /**
     * Connect to the server and call the provided ChannelSftp Closure.
     *
     * @param ftpCredential
     *      - the server to connect to
     * @param closure
     *      - the closure to call
     * @param disconnectOnFinish
     *      - to disconnect the Session when the Closure is done (defaults to true)
     */
    private def connect(FtpCredential ftpCredential, Closure closure, boolean disconnectOnFinish = true) {
        Session session = null
        ChannelSftp sftp = null
        try {
            JSch jSch = new JSch()
            session = jSch.getSession ftpCredential?.username, ftpCredential?.server, ftpCredential?.port
            session.setConfig "StrictHostKeyChecking", "no"
            if (ftpCredential?.password) {
                session.password = ftpCredential?.password
            } else {
                File keyFile = new File("${grailsApplication.config.pathToKeyFile}")
                jSch.addIdentity(keyFile?.absolutePath)
            }
            session.connect()
            Channel sFtpChannel = session.openChannel "sftp"
            sFtpChannel.connect()
            sftp = sFtpChannel as ChannelSftp
            sftp.cd ftpCredential?.remoteBaseDir
            closure.call sftp
        } catch (Exception ex) {
            ex.printStackTrace()
        } finally {
            if (disconnectOnFinish) {
                sftp?.exit()
                session?.disconnect()
            }
        }
    }
}