FTP文件下载-断点续传

FTP文件下载-断点续传

FTP文件下载:https://blog.csdn.net/sll714827/article/details/112802286

package com.example.util.ftp;

import com.example.Service.dataService;
import com.example.pojo.Data;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
public class FTPFileDownload {
    
    



    public Boolean downloadFile(FTPConfig ftpConfig){
    
    

        Boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
    
    
            ftp.connect(ftpConfig.getIpAddr());
            ftp.setControlEncoding("GBK");
            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
            conf.setServerLanguageCode("zh");

            ftp.login(ftpConfig.getUserName(), ftpConfig.getPwd());
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
    
    
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(ftpConfig.getRemotePath());
            FTPFile[] files = ftp.listFiles();
            if (files.length != 1){
    
    
                System.out.println("远程文件不存在!");
            }
            long remoteSize = files[0].getSize();
            File local = new File(ftpConfig.getLocalPath()+ftpConfig.getFileName());
            // 本地存在文件,进行断点下载
            if (local.exists()){
    
    
             long localSize = local.length();
                // 判断本地文件大小是否大于远程文件大小
             if (localSize>=remoteSize){
    
    
                 System.out.println("本地文件大于远程文件,下载中止");
             }
             FileOutputStream outputStream = new FileOutputStream(local,true);
             ftp.setRestartOffset(localSize);
                InputStream inputStream = ftp.retrieveFileStream(new String(files[0].getName().getBytes("GBK"),"ISO-8859-1"));
                byte[] bytes = new byte[1024];
                long step = remoteSize / 100;
                long process = localSize / step;
                int c;
                while ((c = inputStream.read(bytes)) != -1) {
    
    
                    outputStream.write(bytes, 0, c);
                    localSize += c;
                    long nowProcess = localSize / step;
                    if (nowProcess > process) {
    
    
                        process = nowProcess;
                        if (process % 10 == 0)
                            System.out.println("下载进度:" + process);
                        // TODO 更新文件下载进度,值存放在process变量中
                    }
                }
                inputStream.close();
                outputStream.close();
                Boolean flag = ftp.completePendingCommand();
                if (flag){
    
    
                    System.out.println("断点下载文件成功");
                }else{
    
    
                    System.out.println("断点下载文件失败");
                }
            }else{
    
    
                for (int i = 0; i < files.length; i++) {
    
    
                    FTPFile ff = files[i];
                    System.out.println(ff.getName());
                    System.out.println(ftpConfig.getFileName());
                    System.out.println(ff.getName().equals(ftpConfig.getFileName()));
                    if (ff.getName().equals(ftpConfig.getFileName())) {
    
    
                        File localfile = new File(ftpConfig.getLocalPath()+File.separator+ ff.getName());
                        OutputStream is = new FileOutputStream(localfile);
                        ftp.retrieveFile(new String(ff.getName().getBytes("GBK"), "ISO-8859-1"), is);
//                        Data data = new Data(0,ff.getName(),String.valueOf(ff.getSize()),ff.getTimestamp().toString(),String.valueOf(new Date()),"下载完成");
//                        int result = dataService.addData(data);
                        is.close();
                    }
                }
            }
            ftp.logout();
            success = true;
        }catch (Exception e){
    
    
            e.printStackTrace();
        }finally {
    
    
            if (ftp.isConnected()){
    
    
                try{
    
    
                    ftp.disconnect();
                }catch (IOException IOe){
    
    

                }

            }
        }
        return success;
    }
}

猜你喜欢

转载自blog.csdn.net/sll714827/article/details/112802402