久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Java 小程序中顯示 FTP 文件上傳期間的進(jìn)度

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

問題描述

好的,我讓上傳者使用 Java FTP 上傳文件,我想更新標(biāo)簽和進(jìn)度條.帶有百分比文本的標(biāo)簽,帶有百分比 int 值的條形圖.現(xiàn)在使用當(dāng)前代碼只能在上傳結(jié)束時獲得 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);
}

推薦答案

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

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 了解一些進(jìn)一步了解

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

我建議使用 SwingWorker 來執(zhí)行實際轉(zhuǎn)移利用其內(nèi)置的進(jìn)度支持

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 方法,您只需在它的構(gòu)造函數(shù)中進(jìn)行下載調(diào)用.這對您沒有任何幫助...
  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 的實現(xiàn)并將其替換為

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 中,將線程執(zhí)行代碼替換為

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();
}

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

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

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

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
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 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 青青草网站在线观看 | www.日韩| 精品在线| 精品国产乱码久久久久久88av | 欧美日韩精品久久久免费观看 | 亚洲视频二区 | 日本视频中文字幕 | 日韩午夜电影 | 久久亚洲欧美日韩精品专区 | 一本大道久久a久久精二百 国产成人免费在线 | 国产成人在线视频播放 | 国产乱码久久久久久一区二区 | 久久国产激情视频 | 欧美激情视频网站 | 亚洲一级毛片 | 中文字幕专区 | 亚洲天堂成人在线视频 | 成人三级在线播放 | 91精品国产高清久久久久久久久 | 国产精品久久久久久婷婷天堂 | 欧美一区二区三区视频在线播放 | 岛国在线免费观看 | 日韩精品久久一区 | www.天天干.com | 日本成年免费网站 | 亚洲欧美日韩在线 | 久热精品在线观看视频 | 一区二区视频在线 | 欧美狠狠操 | 97起碰| 国内精品成人 | 毛片一区二区三区 | 亚洲视频在线看 | 久久免费福利 | 亚洲精品视频在线播放 | 久草热在线 | 日韩欧美大片 | av一区在线 | 久久精品国产一区二区电影 | av一级久久| 999久久久久久久久6666 |