pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 Java 小程序中顯示 FTP 文件上傳期間的進度

Show progress during FTP file upload in a java applet(在 Java 小程序中顯示 FTP 文件上傳期間的進度)
本文介紹了在 Java 小程序中顯示 FTP 文件上傳期間的進度的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

好的,我讓上傳者使用 Java FTP 上傳文件,我想更新標簽和進度條.帶有百分比文本的標簽,帶有百分比 int 值的條形圖.現在使用當前代碼只能在上傳結束時獲得 100 條和完整條.在上傳期間,它們都沒有改變.

OK so I have the uploader uploading files using the Java FTP, I would like to update the label and the progress bar. Label with the percent text, bar with the percent int value. Right now with the current code only get the 100 and full bar at the end of the upload. During the upload none of them change.

這里是:

    OutputStream output = new BufferedOutputStream(ftpOut);
    CopyStreamListener listener = new CopyStreamListener() {
        public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) {
            System.out.printf("
%-30S: %d / %d", "Sent", totalBytesTransferred, streamSize);
            ftpup.this.upd(totalBytesTransferred,streamSize);
        }
        public void bytesTransferred(CopyStreamEvent arg0) { }
    };

    Util.copyStream(input, output, ftp.getBufferSize(), f.length(), listener);      
}

public void upd(long num, long size){
    int k = (int) ((num*100)/size);
    System.out.println(String.valueOf(k));
    this.d.setText(String.valueOf(k));
    //d.setText(String.valueOf(k));
    progressBar.setValue(k);
}

推薦答案

從它的聲音(并且缺乏任何證據證明)聽起來你在 事件調度線程

From the sounds of it (and lacking any evidence to the contree) it sounds like your processing a time consuming action in the Event Dispatching Thread

您可能想閱讀 Concurrency in Swing 了解一些進一步了解

You might like to read Concurrency in Swing for some further insight

我建議使用 SwingWorker 來執行實際轉移利用其內置的進度支持

I'd suggest using a SwingWorker to perform the actual transfer & take advantage of its built in progress support

看到源代碼后更新

  1. 請勿將重量較重的組件與重量較輕的組件混用.將Applet改為JApplet,將TextField改為JTextField,不要使用Canvas使用 JPanelJComponent
  2. 如果您希望其他人閱讀您的代碼,請為您的變量使用正確的名稱,我不知道 p 是什么.
  3. 你的 Thread 沒用.而不是啟動線程并使用它的 run 方法,您只需在它的構造函數中進行下載調用.這對您沒有任何幫助...
  1. Don't mix heavy weight components with light weight components. Change Applet to JApplet, change TextField to JTextField, don't use Canvas use a JPanel or JComponent
  2. If you expect other people to read your code, please use proper names for your variables, I have no idea what p is.
  3. Your Thread is useless. Rather then starting the thread and using it's run method you simply make your download call within it's constructor. This will do nothing for you...

刪除 MyThread 的實現并將其替換為

Remove your implementation of MyThread and replace it with

public class MyWorker extends SwingWorker<Object, Object> {

    private URL host;
    private File outputFile;

    public MyWorker(URL host, File f) {
        this.host = host;
        outputFile = f;
    }

    @Override
    protected Object doInBackground() throws Exception {

        // You're ignoring the host you past in to the constructor
        String hostName = "localhost";
        String username = "un";
        String password = "pass";
        String location = f.toString();

        //FTPClient ftp = null;

        ftp.connect(hostName, 2121);
        ftp.login(username, password);

        ftp.setFileType(FTP.BINARY_FILE_TYPE);

        ftp.setKeepAlive(true);
        ftp.setControlKeepAliveTimeout(3000);
        ftp.setDataTimeout(3000); // 100 minutes
        ftp.setConnectTimeout(3000); // 100 minutes

        ftp.changeWorkingDirectory("/SSL");

        int reply = ftp.getReplyCode();
        System.out.println("Received Reply from FTP Connection:" + reply);

        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Connected Success");
        }
        System.out.println(f.getName().toString());

        File f1 = new File(location);
        in = new FileInputStream(f1);

        FileInputStream input = new FileInputStream(f1);
        // ftp.storeFile(f.getName().toString(),in);

        //ProgressMonitorInputStream is= new ProgressMonitorInputStream(getParent(), "st", in);
        OutputStream ftpOut = ftp.storeFileStream(f.getName().toString());


        System.out.println(ftpOut.toString());
        //newname hereSystem.out.println(ftp.remoteRetrieve(f.toString()));
        OutputStream output = new BufferedOutputStream(ftpOut);
        CopyStreamListener listener = new CopyStreamListener() {
            public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {

                setProgress((int) Math.round(((double) totalBytesTransferred / (double) streamSize) * 100d));

            }

            @Override
            public void bytesTransferred(CopyStreamEvent arg0) {
                // TODO Auto-generated method stub
            }
        };

        Util.copyStream(input, output, ftp.getBufferSize(), f.length(), listener);

        return null;

    }
}

o (??) 的 ActionListener 中,將線程執行代碼替換為

In your ActionListener of o (??) replace the thread execution code with

try {
    MyWorker worker = new MyWorker(new URL("http://localhost"), file);
    worker.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("progress")) {
                Integer progress = (Integer) evt.getNewValue();
                progressBar.setValue(progress);
            }
        }
    });
    worker.execute();
} catch (MalformedURLException ex) {
    ex.printStackTrace();
}

注意.您忽略了傳遞給構造函數的 URL.http://不是 ftp://所以我懷疑這會起作用......

Note. You are ignoring the URL you pass to the constructor. http:// is not ftp:// so I doubt this will work...

這篇關于在 Java 小程序中顯示 FTP 文件上傳期間的進度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 儿童乐园|游乐场|淘气堡招商加盟|室内儿童游乐园配套设备|生产厂家|开心哈乐儿童乐园 | 铜镍-康铜-锰铜-电阻合金-NC003 - 杭州兴宇合金有限公司 | 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 线材成型机,线材折弯机,线材成型机厂家,贝朗自动化设备有限公司1 | 茅茅虫AI论文写作助手-免费AIGC论文查重_写毕业论文降重 | 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 石家庄律师_石家庄刑事辩护律师_石家庄取保候审-河北万垚律师事务所 | 顺辉瓷砖-大国品牌-中国顺辉| 铝镁锰板厂家_进口钛锌板_铝镁锰波浪板_铝镁锰墙面板_铝镁锰屋面-杭州军晟金属建筑材料 | 上海深蓝_缠绕机_缠膜机-上海深蓝机械装备有限公司 | 宝元数控系统|对刀仪厂家|东莞机器人控制系统|东莞安川伺服-【鑫天驰智能科技】 | 集装箱标准养护室-集装箱移动式养护室-广州璟业试验仪器有限公司 | 橡胶接头_橡胶软接头_可曲挠橡胶接头-巩义市创伟机械制造有限公司 | 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 | 舞台木地板厂家_体育运动木地板_室内篮球馆木地板_实木运动地板厂家_欧氏篮球地板推荐 | 电镀电源整流器_高频电解电源_单脉双脉冲电源 - 东阳市旭东电子科技 | 医养体检包_公卫随访箱_慢病随访包_家签随访包_随访一体机-济南易享医疗科技有限公司 | 浙江栓钉_焊钉_剪力钉厂家批发_杭州八建五金制造有限公司 | 代理记账_免费注册公司_营业执照代办_资质代办-【乐财汇】 | 壹作文_中小学生优秀满分作文大全 | 北京网站建设-企业网站建设-建站公司-做网站-北京良言多米网络公司 | 心得体会网_心得体会格式范文模板| 薪动-人力资源公司-灵活用工薪资代发-费用结算-残保金优化-北京秒付科技有限公司 | 喷砂机厂家_自动喷砂机生产_新瑞自动化喷砂除锈设备 | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | 桑茶-七彩贝壳桑叶茶 长寿茶 | 医用酒精_84消毒液_碘伏消毒液等医用消毒液-漓峰消毒官网 | ICP备案查询_APP备案查询_小程序备案查询 - 备案巴巴 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 牛奶检测仪-乳成分分析仪-北京海谊 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | 电梯装饰-北京万达中意电梯装饰有限公司 | 药品/药物稳定性试验考察箱-埃里森仪器设备(上海)有限公司 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 |