0

ここに来る前に、私はウェブ全体を検索し、これについて話している何十ものトピックを読みましたが、問題を解決できません.

アップロードの進行状況を表示したい。次のコードでは、JFrame が更新されないことを除いて、すべてが機能します。別のトピックで見つけた手法を使用していますが、うまくいかないようです。私のコードを見ていただくと、より簡単になると思います(問題に関係のない指示を消去しました)。

/*
 * Correct imports have been done
 */

class GUI extends JFrame {
    public JPanel pan;

    public GUI(JPanel panel) {
        super("Uploading...");
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setPreferredSize(new Dimension(600, 500));
        setMinimumSize(new Dimension(600, 500));
        setMaximumSize(new Dimension(600, 500));
        setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
        setLocationRelativeTo(null);

        pan = panel;
        pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));

        setContentPane(pan);
        setVisible(true);   
    }
}

public class GUIUpload {
    private static GUI ui;

    public static void main(String args[]) {
        JPanel main = new JPanel();
        ui = new GUI(main); // create and display GUI        
        uploadLoop(args, main); // start the upload loop

        /*
         * After upload is finished
         */
            JButton jb = new JButton("Ok");
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                ui.setVisible(false);               
            }
        });

        ui.getContentPane().add(jb);
        ui.getContentPane().repaint();
    }

    private static void uploadLoop(String[] paths, JPanel monitor) {
        /*
         * Upload starts here
         */
        long transfered;
        long size;
        InputStream inputStream;        

        FTPClient ftpClient = new FTPClient();
        try {
        ftpClient.connect("xxxxxx", 21);
            boolean success = ftpClient.login("xxxxxx", "xxxxxx");
            /*
             * Sending
             */
            for (int i = 0; i < 4; i++){
                if (paths[i] != null){
                    File localFile = new File(paths[i]);
                    String remoteFile = "/public_html/papers/" + i + ".pdf";
                    JLabel label = new JLabel("Uploading...");
                    ui.getContentPane().add(label);
                    ui.repaint();

                    inputStream = new FileInputStream(localFile);   
                    // Monitoring misc
                    size = localFile.length();
                    transfered = 0;
                    int percentage = 0;
                                    // Progress bar
                    JProgressBar pgb = new JProgressBar();
                    pgb.setValue(0);
                    ui.getContentPane().add(pgb);
                    ui.repaint();
                    // Upload routine
                    OutputStream outputStream = ftpClient.storeFileStream(remoteFile);;
                    byte[] bytesIn = new byte[4096];
                    int read = 0;
                    while ((read = inputStream.read(bytesIn)) != -1) {
                        outputStream.write(bytesIn, 0, read);
                        transfered += read;
                        percentage = (int)(transfered * 100.0 / size + 0.5);
                        System.out.println(percentage);
                        pgb.setValue(percentage);
                        ui.repaint();
                    }
                    inputStream.close();
                    outputStream.close();
                    boolean completed = ftpClient.completePendingCommand();
                    /*
                     * End of upload
                     */ 
                }   
            }
        } // end try
        catch (Exception e){
            // Do nothing}
        } // end catch
    } // end upload method
}

パーセンテージは正常に機能します。ファイル転送は正常に機能します。GUI フレームは、GUIUpload クラスのメイン メソッドで再描画した後にのみ更新されます。再描画すると、すべてのラベルと進行状況バーが正しく追加および更新されていることがわかります (進行状況バーは最大値を示しています。

だから..これを行う方法を探しているのはかなりの時間であり、スレッドを使用してみました。多くのことを試しましたが、うまくいきませんでした(または、それらを試したときに何か間違ったことをしました)。

私を助けてくれる人に感謝します。

よろしくお願いします。

4

1 に答える 1