⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileuploadmanagerthread.java

📁 [linux.rar] - 嵌入式linux开发教程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                this.uploadPolicy.sendDebugInformation("Error in Upload",
                        getUploadException());
            }

            // If the upload was successful, we wait for 5 seconds, before
            // clearing the progress bar.
            if (!isUploadStopped() && getUploadException() != null) {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    // Nothing to do
                }
            }
            // The job is finished for long enough, let's clear the progression
            // bars.
            this.preparationProgressBar.setValue(0);
            this.preparationProgressBar.setString("");
            this.uploadProgressBar.setValue(0);
            this.uploadProgressBar.setString("");

            this.uploadPolicy.displayDebug(
                    "End of the FileUploadManagerThread", 5);
        } catch (JUploadException jue) {
            // Let's have a little information.
            if (this.uploadException == null) {
                this.uploadException = jue;
            }
            this.uploadPolicy.displayErr(
                    "Uncaught exception in FileUploadManagerThread/run()", jue);

            // And go back into a 'normal' way.
            stopUpload();
        } finally {
            // We restore the button state, just to be sure.
            this.uploadPanel.updateButtonState();
        }

        this.timerProgressBar.stop();
        this.timerStatusBar.stop();

        // And we die of our beautiful death ... until next upload.
    }// run

    /**
     * Get the exception that occurs during upload.
     * 
     * @return The exception, or null if no exception were thrown.
     */
    public Exception getException() {
        return this.uploadException;
    }

    /**
     * Get the total number of files which have been successfully uploaded.
     * 
     * @return Total number of uploaded files.
     */
    public int getNbUploadedFiles() {
        return this.nbSentFiles;
    }

    /**
     * Retrieve the start time of the upload. That is: when the first upload
     * starts. It can be some delay after this thread creation, as it first need
     * to prepare files to upload.
     * 
     * @return The time this thread was started in ms.
     */
    public final long getUploadStartTime() {
        return this.uploadStartTime;
    }

    /**
     * Stores the last upload exception that occurs. This method won't write to
     * the log file.
     * 
     * @param uploadException
     */
    public void setUploadException(JUploadException uploadException) {
        // We don't override an existing exception
        if (this.uploadException != null) {
            this.uploadPolicy
                    .displayWarn("An exception has already been set in FileUploadManagerThread. The next one is just logged.");
        } else {
            this.uploadException = uploadException;
        }

        this.uploadPolicy.displayErr(uploadException);
        this.preparationProgressBar.setString(uploadException.getMessage());
        // We stop the upload as soon as an error occurs.
        this.uploadFinished = true;
    }

    /**
     * Get the last upload exception that occurs.
     * 
     * @return The last upload exception, or null if no exception occurs.
     */
    public JUploadException getUploadException() {
        return this.uploadException;
    }

    /**
     * Indicates whether the upload is finished or not. As several conditions
     * can make the upload being finished (all files uploaded, an error occured,
     * the user stops the upload), a specific boolean is built. It's managed by
     * the {@link #run()} method.
     * 
     * @return true if the upload is finished. False otherwise.
     */
    public boolean isUploadFinished() {
        // Indicate whether or not the upload is finished. Several conditions.
        return this.uploadFinished;
    }

    /**
     * Indicates if the upload has been stopped by the user, or by any upload
     * error.
     * 
     * @return true if the current upload has been asked to stop by the user,
     *         false otherwise.
     */
    public boolean isUploadStopped() {
        return this.stop;
    }

    /**
     * Used by the UploadFileData#uploadFile(java.io.OutputStream, long) for
     * each uploaded buffer
     * 
     * @param nbBytes Number of additional bytes that where uploaded.
     * @throws JUploadException
     */
    public synchronized void nbBytesUploaded(long nbBytes)
            throws JUploadException {
        this.nbUploadedBytes += nbBytes;
        this.nbBytesUploadedForCurrentFile += nbBytes;
    }

    /**
     * Indicate the current state of the upload, to allow a correct display of
     * the upload progress bar.
     * 
     * @param numOfFileInCurrentRequest
     * @param uploadStatus
     * @throws JUploadException
     */
    public synchronized void setUploadStatus(int numOfFileInCurrentRequest,
            int uploadStatus) throws JUploadException {
        if (globalStartTime == 0) {
            // Ok, the upload just starts. We keep the date, to later calculate
            // the ETA.
            globalStartTime = System.currentTimeMillis();
        }
        if (uploadStatus == UPLOAD_STATUS_UPLOADED_WAITING_FOR_RESPONSE) {
            // We're waiting for the server: let's add it to the sending
            // duration.
            uploadDuration += System.currentTimeMillis()
                    - currentRequestStartTime;
            currentRequestStartTime = 0;
        } else if (uploadStatus == UPLOAD_STATUS_UPLOADING
                && currentRequestStartTime == 0) {
            currentRequestStartTime = System.currentTimeMillis();
        }
        this.numOfFileInCurrentRequest = numOfFileInCurrentRequest;
        this.uploadStatus = uploadStatus;

        this.updateUploadProgressBar();
    }

    /**
     * Reaction to the user click on the 'Stop' button, or any action from the
     * user asking to stop the upload. The upload should go on for the current
     * file, and stop before starting the next upload request to the server, to
     * avoid strange problems on the server.
     */
    public void stopUpload() {
        this.stop = true;

        // The upload is now finished ...
        this.uploadFinished = true;
    }

    // //////////////////////////////////////////////////////////////////////////////////////////////
    // /////////////////// TIMER MANAGEMENT
    // (to display the current upload status in the status bar)
    // //////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof Timer) {
            // If the upload is finished, we stop the timer here.
            if (isUploadFinished()) {
                this.timerProgressBar.stop();
            }
            if (e.getSource() == timerProgressBar) {
                updateUploadProgressBar();
            }
            if (e.getSource() == timerStatusBar) {
                updateUploadStatusBar();
            }
        }
    }

    /**
     * Displays the current upload speed on the status bar.
     */
    private void updateUploadStatusBar() {
        // We'll update the status bar, only if it exists and if the upload
        // actually started.
        if (null != this.uploadPanel.getStatusLabel()
                && getUploadStartTime() != 0 && this.nbUploadedBytes > 0) {
            // actualUploadDuration: contains the sum of the time, when the
            // applet is actually sending data to the server, and ignores the
            // time when it waits for the server's response.
            // This is used to calculate the upload speed.
            long actualUploadDuration;
            double totalFileBytesToSend;
            double percent;
            // uploadCPS: contains the upload speed.
            double uploadSpeed;
            // globalCPS: contains the average speed, including the time the
            // applet is waiting for the server response.
            double globalCPS;
            long remaining;
            String eta;

            // Let's calculate the actual upload duration. That is: the time
            // during which we're really sending data to the server.
            if (currentRequestStartTime == 0) {
                // We're currently sending nothing to the server.
                actualUploadDuration = uploadDuration;
            } else {
                // We're currently sending data to the server. We add the time
                // of the current request to the stored upload duration.
                actualUploadDuration = uploadDuration
                        + System.currentTimeMillis() - currentRequestStartTime;
            }
            // For next steps, we expect a duration in seconds:
            actualUploadDuration /= 1000;

            // Let's estimate the total, or calculate it, of all files are
            // prepared
            if (this.nbPreparedFiles == this.uploadFileDataArray.length) {
                // All files are prepared: it's no more an estimation !
                totalFileBytesToSend = this.nbTotalNumberOfPreparedBytes;
            } else {
                // We sum the total number of prepared bytes, and we estimate
                // the size of the files that are not prepared yet
                totalFileBytesToSend = this.nbTotalNumberOfPreparedBytes
                        +
                        // And we sum it with the average amount per file
                        // prepared for the others
                        (this.uploadFileDataArray.length - this.nbPreparedFiles)
                        * this.nbTotalNumberOfPreparedBytes
                        / this.nbPreparedFiles;
            }
            try {
                percent = 100.0 * this.nbUploadedBytes / totalFileBytesToSend;
            } catch (ArithmeticException e1) {
                percent = 100;
            }

            // Calculation of the 'pure' upload speed.
            try {
                uploadSpeed = this.nbUploadedBytes / actualUploadDuration;
            } catch (ArithmeticException e1) {
                uploadSpeed = this.nbUploadedBytes;
            }

            // Calculation of the 'global' upload speed.
            try {
                globalCPS = this.nbUploadedBytes
                        / (System.currentTimeMillis() - this.globalStartTime)
                        * 1000;
            } catch (ArithmeticException e1) {
                globalCPS = this.nbUploadedBytes;
            }

            // Calculation of the ETA. It's based on the global upload speed.
            try {
                remaining = (long) ((totalFileBytesToSend - this.nbUploadedBytes) / globalCPS);
                if (remaining > 3600) {
                    eta = String.format(this.uploadPolicy
                            .getString("timefmt_hms"), new Long(
                            remaining / 3600), new Long((remaining / 60) % 60),
                            new Long(remaining % 60));
                } else if (remaining > 60) {
                    eta = String.format(this.uploadPolicy
                            .getString("timefmt_ms"), new Long(remaining / 60),
                            new Long(remaining % 60));
                } else
                    eta = String.format(this.uploadPolicy
                            .getString("timefmt_s"), new Long(remaining));
            } catch (ArithmeticException e1) {
                eta = this.uploadPolicy.getString("timefmt_unknown");
            }
            String format = this.uploadPolicy.getString("status_msg");
            String status = String.format(format, new Integer((int) percent),
                    SizeRenderer.formatFileUploadSpeed(uploadSpeed,
                            this.uploadPolicy), eta);
            this.uploadPanel.getStatusLabel().setText(status);
            // this.uploadPanel.getStatusLabel().repaint(100);
            this.uploadPolicy.getApplet().getAppletContext().showStatus(status);
            // this.uploadPolicy.displayDebug("[updateUploadStatusBar] " +
            // status, 101);
        }
    }

    // //////////////////////////////////////////////////////////////////////////////////////////////
    // /////////////////// SYNCHRONIZATION METHODS
    // //////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * Check if a new packet is ready. A packet is ready if enough file are
     * prepared for upload (compared to the nbFilesPerRequest applet parameter)
     * or if the sum of bytes for the prepared files are more than the
     * maxChunkSize applet parameter (if it was given as an applet parameter).
     * The result is stored in the {@link #nextPacket} attribute. <BR>
     * Note: Take care that the result of this method (isNextPacketReady)
     * doesn't take into account the number of files that are actually being
     * uploaded.
     * 
     * @throws JUploadException
     */
    private synchronized boolean checkIfNextPacketIsReady()
            throws JUploadException {

        if (this.nextPacket != null) {
            // Nothing to do: the next packet is already prepared
        } else if (this.nbSentFiles + this.nbFilesBeingUploaded == this.nbPreparedFiles) {
            // No file is ready to sent: Upload is finished or all files are
            // been sent to the FileUploadThread.
        } else {
            // Some new files are ready, let's look if the packet is ready. We
            // add at least the first one.
            int maxPacketSize = Math.min(this.nbPreparedFiles
                    - this.nbSentFiles - this.nbFilesBeingUploaded,
                    this.nbFilesPerRequest);
            FileData[] tempFileData = new FileData[maxPacketSize];
            int nbFilesInPacket = 0;
            long packetLength = 0;
            boolean isPacketFinished = false;
            FileData currentFileData;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -