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

📄 fileuploadthreadftp.java

📁 [linux.rar] - 嵌入式linux开发教程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

                if (!FTPReply.isPositiveCompletion(this.ftp.getReplyCode()))
                    throw new JUploadException("Invalid directory specified");

                this.bConnected = true;
            } catch (Exception e) {
                throw new JUploadIOException("Could not connect to server ("
                        + e.getMessage() + ")", e);
            }
        } // if(!bConnected)
    }

    /** @see DefaultFileUploadThread#afterFile(int) */
    @Override
    void afterFile(int index) {
        // Nothing to do
    }

    /** @see DefaultFileUploadThread#beforeFile(int) */
    @Override
    void beforeFile(int index) throws JUploadException {
        try {
            // if configured to, we go to the relative sub-folder of the current
            // file, or on the root of the postURL.
            if (uploadPolicy.getFtpCreateDirectoryStructure()) {
                this.ftp.changeWorkingDirectory(this.filesToUpload[index]
                        .getRelativeDir());
                this.uploadPolicy.displayDebug(this.ftp.getReplyString(), 80);
            } else {
                this.ftp.changeWorkingDirectory(this.dir);
                this.uploadPolicy.displayDebug(this.ftp.getReplyString(), 80);
            }

            setTransferType(index);
            // just in case, delete anything that exists

            // No delete, as the user may not have the right for that. We use,
            // later, the store command:
            // If the file already exists, it will be replaced.
            // ftp.deleteFile(filesToUpload[index].getFileName());

            // Let's open the stream for this file.
            this.ftpOutputStream = this.ftp
                    .storeFileStream(this.filesToUpload[index].getFileName());
            // The upload is done through a BufferedOutputStream. This speed up
            // the upload in an unbelievable way ...
            this.bufferedOutputStream = new BufferedOutputStream(
                    this.ftpOutputStream);
        } catch (IOException e) {
            throw new JUploadException(e);
        }
    }

    /** @see DefaultFileUploadThread#cleanAll() */
    @Override
    void cleanAll() {
        try {
            if (this.ftp.isConnected()) {
                this.ftp.disconnect();
                this.uploadPolicy.displayDebug("disconnected", 50);
            }
        } catch (IOException e) {
            // then we arent connected
            this.uploadPolicy.displayDebug("Not connected", 50);
        } finally {
            this.ftpOutputStream = null;
            this.bufferedOutputStream = null;
        }
    }

    /** @see DefaultFileUploadThread#cleanRequest() */
    @Override
    void cleanRequest() throws JUploadException {
        if (this.bufferedOutputStream != null) {
            try {
                this.bufferedOutputStream.close();
                this.ftpOutputStream.close();
                if (!this.ftp.completePendingCommand()) {
                    throw new JUploadExceptionUploadFailed(
                            "ftp.completePendingCommand() returned false");
                }
            } catch (IOException e) {
                throw new JUploadException(e);
            } finally {
                this.bufferedOutputStream = null;
            }
        }
    }

    /**
     * @throws JUploadIOException
     * @see DefaultFileUploadThread#finishRequest()
     */
    @Override
    int finishRequest() throws JUploadException {
        try {
            getOutputStream().flush();
            return 200;
        } catch (IOException ioe) {
            throw new JUploadIOException("FileUploadThreadFTP.finishRequest()",
                    ioe);
        } catch (Exception e) {
            // When the user may not override an existing file, I got a
            // NullPointerException. Let's trap all errors here.
            throw new JUploadException(
                    "FileUploadThreadFTP.finishRequest()  (check the user permission on the server)",
                    e);
        }
    }

    /** @see DefaultFileUploadThread#getAdditionnalBytesForUpload(int) */
    @Override
    long getAdditionnalBytesForUpload(int indexFile) {
        // Default: no additional byte.
        return 0;
    }

    /** @see DefaultFileUploadThread#getOutputStream() */
    @Override
    OutputStream getOutputStream() {
        return this.bufferedOutputStream;
    }

    /** @see DefaultFileUploadThread#startRequest(long, boolean, int, boolean) */
    @Override
    void startRequest(long contentLength, boolean bChunkEnabled, int chunkPart,
            boolean bLastChunk) {
        // Nothing to do
    }

    /**
     * Will set the binary/ascii value based on the parameters to the applet.
     * This could be done by file extension too but it is not implemented.
     * 
     * @param index The index of the file that we want to upload, in the array
     *            of files to upload.
     * @throws IOException if an error occurs while setting mode data
     */
    private void setTransferType(int index) throws JUploadIOException {
        try {
            // read the value given from the user
            if (this.uploadPolicy.getFtpTransfertBinary()) {
                this.ftp.setFileType(FTP.BINARY_FILE_TYPE);
            } else {
                this.ftp.setFileType(FTP.ASCII_FILE_TYPE);
            }
        } catch (IOException ioe) {
            throw new JUploadIOException(
                    "Cannot set transfert binary or ascii mode (binary: "
                            + this.uploadPolicy.getFtpTransfertBinary() + ")",
                    ioe);
        }

        try {
            // now do the same for the passive/active parameter
            if (this.uploadPolicy.getFtpTransfertPassive()) {
                this.ftp.enterRemotePassiveMode();
                this.ftp.enterLocalPassiveMode();
            } else {
                this.ftp.enterLocalActiveMode();

                this.ftp.enterRemoteActiveMode(
                        InetAddress.getByName(this.host), Integer
                                .parseInt(this.port));
            }
        } catch (IOException ioe) {
            throw new JUploadIOException(
                    "Cannot set transfert passive or active mode (passive: "
                            + this.uploadPolicy.getFtpTransfertBinary() + ")",
                    ioe);

        }
    }

    /**
     * Create all relative sub-directories, so the structure on the server
     * reflects the structure of the uploaded files.
     * 
     * @throws JUploadIOException When an error occurs during folder creation
     */
    // A tester
    private void createDirectoryStructure() throws JUploadIOException {
        // We expect the files are sorted in a relevant order, which allow the
        // creation of sub-directories in the same order.
        for (int i = 0; i < this.filesToUpload.length
                && !this.fileUploadManagerThread.isUploadStopped(); i++) {
            try {
                this.ftp.changeWorkingDirectory(this.filesToUpload[i]
                        .getRelativeDir());
            } catch (IOException ioe) {
                // The directory doesn't exist, we try to create it.
                try {
                    this.ftp.makeDirectory(this.filesToUpload[i]
                            .getRelativeDir());
                    this.uploadPolicy.displayDebug(this.ftp.getReplyString(),
                            80);
                } catch (IOException ioe2) {
                    // Hum, the directory creation crashes.
                    this.uploadPolicy
                            .displayDebug(this.ftp.getReplyString(), 1);
                    throw new JUploadIOException(
                            "(FTP) Erreur while creating the "
                                    + this.filesToUpload[i].getRelativeDir(),
                            ioe2);
                }
            }
        }
    }
}

⌨️ 快捷键说明

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