使用JSCH中的SFTP上传文件,我想知道如何显示有关上传文件的进度

发布于 2025-02-10 00:39:32 字数 406 浏览 0 评论 0原文

我的网页有一个按钮可以选择文件,当按钮单击时,它将将文件数据发送到WebServer,然后Web服务器将构建由JSCH构建的SFTP服务,然后将文件发送到远程服务器。要知道如何开发进度栏。当文件发送到Web服务器时,我已经开发了进度栏。当文件发送到远程服务器时,我尝试开发进度栏,但我失败了。 在此处输入图像描述

图片是将文件发送到WebServer的过程。 图2是将文件发送到远程服务器的过程。 在此处输入图像描述

my web page have a button to select the files,when the button clicked,it will send files data to webserver,then webserver will build a SFTP service which is built from JSCH,then the files will be sent to remote server.now I want to know how to develop the progress bar.I have already developed the progress bar when files send to web server.I try to develop the progress bar when files send to remote server but i failed. enter image description here

picture one is the process which sends files to webserver.
picture two is the process which sends files to remote server.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2025-02-17 00:39:32
// here is code which sends files to webserver. 
$(function() {

    $('#inputButton').on('click', function() {
        var fd = new FormData();
        var files_upload_num = document.getElementById('fileToUpload').files.length;
        if(files_upload_num == 0)
        {
            alert('请选择文件,完成上传!');
            return;
        }
        for(let i=0;i<files_upload_num;i++)
            fd.append("fileToUpload", document.getElementById('fileToUpload').files[i]);
        $.ajax({
            url: '<%=path%>/FileCl?method=submitFile',
            type: 'post',
            data: fd,
            processData: false,
            contentType: false,
            xhr: function() {
                var newxhr;
                if (window.XMLHttpRequest) {
                    newxhr = new XMLHttpRequest();
                } else {
                    // code for IE6, IE5
                    newxhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                newxhr.upload.onprogress = function(e) {
                    console.log(e)
                    var percent = ( (e.loaded / e.total).toFixed(2) ) * 100 + '%'
                    $('#progressBar').css('width', percent)
                    document.getElementById('progressBar').innerHTML = percent;
                }
                newxhr.addEventListener("load", uploadComplete, false);
                newxhr.addEventListener("error", uploadFailed, false);
                newxhr.addEventListener("abort", uploadCanceled, false);
                return newxhr
            },
            success: function(res) {
                console.log(res)
            },
            dataType: 'json'
        })
    })
})

//这是将文件发送到远程服务器的代码。

        JSch jSch = new JSch();
        Session jSchSession = null;
        ChannelSftp chSftp = null;

        jSchSession = jSch.getSession(username, host, port);


        jSchSession.setPassword(password);


        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        jSchSession.setConfig(config);


        jSchSession.setTimeout(1000*10);


        jSchSession.connect();


        chSftp = (ChannelSftp)jSchSession.openChannel("sftp");

        chSftp.connect();

        chSftp.setFilenameEncoding("UTF-8");



        sftpUtil sftpUtil = new sftpUtil(chSftp);


        for (FileItem item : fileItemList) {
            String filename = item.getName();
            if (filename == null || filename.trim().equals("")) {
                continue;
            }
            String id = UUID.randomUUID().toString().replace("-","");
            id = id.substring(0,8);

            File saveFile = new File(path,id+filename);
            fileNameList.add(id+filename);
            sftpUtil.upload("/home/liuyb/uploads",filename,item.getInputStream(),new uploadFileProgressMonitor(item.getSize()));
        }
// here is code which sends files to webserver. 
$(function() {

    $('#inputButton').on('click', function() {
        var fd = new FormData();
        var files_upload_num = document.getElementById('fileToUpload').files.length;
        if(files_upload_num == 0)
        {
            alert('请选择文件,完成上传!');
            return;
        }
        for(let i=0;i<files_upload_num;i++)
            fd.append("fileToUpload", document.getElementById('fileToUpload').files[i]);
        $.ajax({
            url: '<%=path%>/FileCl?method=submitFile',
            type: 'post',
            data: fd,
            processData: false,
            contentType: false,
            xhr: function() {
                var newxhr;
                if (window.XMLHttpRequest) {
                    newxhr = new XMLHttpRequest();
                } else {
                    // code for IE6, IE5
                    newxhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                newxhr.upload.onprogress = function(e) {
                    console.log(e)
                    var percent = ( (e.loaded / e.total).toFixed(2) ) * 100 + '%'
                    $('#progressBar').css('width', percent)
                    document.getElementById('progressBar').innerHTML = percent;
                }
                newxhr.addEventListener("load", uploadComplete, false);
                newxhr.addEventListener("error", uploadFailed, false);
                newxhr.addEventListener("abort", uploadCanceled, false);
                return newxhr
            },
            success: function(res) {
                console.log(res)
            },
            dataType: 'json'
        })
    })
})

//here is code which sends files to remote server.

        JSch jSch = new JSch();
        Session jSchSession = null;
        ChannelSftp chSftp = null;

        jSchSession = jSch.getSession(username, host, port);


        jSchSession.setPassword(password);


        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        jSchSession.setConfig(config);


        jSchSession.setTimeout(1000*10);


        jSchSession.connect();


        chSftp = (ChannelSftp)jSchSession.openChannel("sftp");

        chSftp.connect();

        chSftp.setFilenameEncoding("UTF-8");



        sftpUtil sftpUtil = new sftpUtil(chSftp);


        for (FileItem item : fileItemList) {
            String filename = item.getName();
            if (filename == null || filename.trim().equals("")) {
                continue;
            }
            String id = UUID.randomUUID().toString().replace("-","");
            id = id.substring(0,8);

            File saveFile = new File(path,id+filename);
            fileNameList.add(id+filename);
            sftpUtil.upload("/home/liuyb/uploads",filename,item.getInputStream(),new uploadFileProgressMonitor(item.getSize()));
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文