Java Code Examples for org.apache.commons.net.ftp.FTPClient#setRestartOffset()

The following examples show how to use org.apache.commons.net.ftp.FTPClient#setRestartOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UploadThread.java    From imageServer with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件到服务器,新上传和断点续传
 *
 * @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
 * @param localFile  本地文件 File句柄,绝对路径
 * @param ftpClient  FTPClient 引用
 * @param remoteSize 远程已上传的文件大小
 * @return
 * @throws IOException
 */
private boolean uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException {
    //显示进度的上传
    long step = localFile.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    RandomAccessFile raf = new RandomAccessFile(localFile, "r");
    OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
    //断点续传
    if (remoteSize > 0) {
        ftpClient.setRestartOffset(remoteSize);
        process = remoteSize / step;
        raf.seek(remoteSize);
        localreadbytes = remoteSize;
    }
    /* 每一次上传的数据量 */
    byte[] bytes = new byte[1024*10];
    int c;
    while ((c = raf.read(bytes)) != -1) {
        out.write(bytes, 0, c);
        localreadbytes += c;
        if (localreadbytes / step != process) {
            process = localreadbytes / step;
            log.info("上传进度:" + process);
        }
    }
    out.flush();
    raf.close();
    out.close();
    return ftpClient.completePendingCommand();
}
 
Example 2
Source File: UploadThread.java    From imageServer with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件到服务器,新上传和断点续传
 *
 * @param pipedOutputStream 输出的连接管道流
 * @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
 * @param localFile  本地文件 File句柄,绝对路径
 * @param ftpClient  FTPClient 引用
 * @param remoteSize 远程已上传的文件大小
 * @return
 * @throws IOException
 */
private boolean uploadFile(PipedOutputStream pipedOutputStream,String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException {
    //显示进度的上传
    long step = localFile.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    RandomAccessFile raf = new RandomAccessFile(localFile, "r");
    OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
    //断点续传
    if (remoteSize > 0) {
        ftpClient.setRestartOffset(remoteSize);
        process = remoteSize / step;
        raf.seek(remoteSize);
        localreadbytes = remoteSize;
    }
    /* 每一次上传的数据量 */
    byte[] bytes = new byte[1024*10];
    int c;
    while ((c = raf.read(bytes)) != -1) {
        out.write(bytes, 0, c);
        localreadbytes += c;
        if (localreadbytes / step != process) {
            process = localreadbytes / step;
            pipedOutputStream.write(Integer.valueOf(process+""));
            pipedOutputStream.flush();
            log.info("上传进度:" + process);
        }
    }
    out.flush();
    out.close();
    raf.close();
    return ftpClient.completePendingCommand();
}
 
Example 3
Source File: FtpContinueClient.java    From spring-boot-study with MIT License 4 votes vote down vote up
/**
 * 上传文件到服务器,新上传和断点续传
 *
 * @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变
 * @param localFile 本地文件File句柄,绝对路径
 * @param ftpClient FTPClient引用
 * @return
 * @throws IOException
 */
public UploadStatus uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize)
        throws IOException {
    UploadStatus status;
    // 显示进度的上传
    long step = localFile.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    RandomAccessFile raf = new RandomAccessFile(localFile, "r");
    OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
    // 断点续传
    if (remoteSize > 0)
    {
        ftpClient.setRestartOffset(remoteSize);
        process = remoteSize / step;
        raf.seek(remoteSize);
        localreadbytes = remoteSize;
    }
    byte[] bytes = new byte[1024];
    int c;
    while ((c = raf.read(bytes)) != -1)
    {
        out.write(bytes, 0, c);
        localreadbytes += c;
        if (localreadbytes / step != process)
        {
            process = localreadbytes / step;
            System.out.println("上传进度:" + process);
            // TODO 汇报上传状态
        }
    }
    out.flush();
    raf.close();
    out.close();
    boolean result = ftpClient.completePendingCommand();
    if (remoteSize > 0)
    {
        status = result ? UploadStatus.Upload_From_Break_Success : UploadStatus.Upload_From_Break_Failed;
    }
    else
    {
        status = result ? UploadStatus.Upload_New_File_Success : UploadStatus.Upload_New_File_Failed;
    }

    return status;
}