Javaは定期的にローカルファイルをFTPサーバーに自動的にアップロードします

Javaは定期的にローカルファイルをFTPサーバーに自動的にアップロードします

参照ツールコンポーネント: hutool

package cn.dexter.filesync;

import cn.dexter.filesync.metadata.sync.util.PropertiesUtils;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ftp.Ftp;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ftp文件上传服务
 * 定时扫描文件路径上传至ftp服务器
 *
 * @author Dexter
 */
public class FtpUploadThread extends Thread {
    
    

    String url = PropertiesUtils.getString("ftp.url");
    String username = PropertiesUtils.getString("ftp.username");
    String password = PropertiesUtils.getString("ftp.password");
    /**
     * 本地扫描路径
     */
    String messageLocalPath = PropertiesUtils.getString("file.localPath");
    /**
     * 本地服务端主题,用于区分文件来源
     */
    String svrTopic = PropertiesUtils.getString("svrTopic");

    @Override
    public void run() {
    
    
        try {
    
    
            List<File> fileList = FileUtil.loopFiles(messageLocalPath);
            if (fileList.size() > 0) {
    
    
                LocalDateTime starttime = LocalDateTime.now();
                Ftp ftp = new Ftp(url, 21, username, password);
                //开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就用本地编码(ISO-8859-1)
                if (FTPReply.isPositiveCompletion(ftp.getClient().sendCommand("OPTS UTF8", "ON"))) {
    
    
                    ftp.getClient().setControlEncoding("UTF-8");
                } else {
    
    
                   //FTP协议里面,规定文件名编码为iso-8859-1
                    ftp.getClient().setControlEncoding("ISO-8859-1");
                }
                //设置被动模式
                ftp.getClient().enterLocalActiveMode();


                for (File file : fileList) {
    
    
                    if (file.isFile()) {
    
    
                        //每次上传后切换操作路径为根路径
                        ftp.cd("/");
                        //读取文件绝对路径
                        String absolutePath = file.getAbsolutePath().replace("\\", "/");
                        String fileName = file.getName();
                        //替换ftp服务器远程根路径
                        String remotePath = absolutePath.replace(messageLocalPath, "").replace(fileName, "");
                        if (StrUtil.isNotBlank(svrTopic)) {
    
    
                            //添加服务器主题
                            remotePath = svrTopic + "/" + remotePath;
                        }
                        try {
    
    
                            if (ftp.upload(remotePath, fileName, file)) {
    
    
                                //文件上传成功后,删除本地数据。
                                FileUtil.del(file);
                            }
                        } catch (IORuntimeException e) {
    
    
                            System.out.println(String.format("上传文件[%s]失败!", absolutePath));
                        }
                    }

                }
                ftp.close();
                LocalDateTime endttime = LocalDateTime.now();
                Duration duration = Duration.between(starttime, endttime);
                System.out.println(String.format(" 本次文件同步耗时:%s分钟 %s秒", duration.toMinutes(), duration.toMillis() / 1000));
            }
        } catch (Exception e) {
    
    
            System.out.println(String.format("上传文件至ftp服务器异常:%s", e.getMessage()));
        }
    }
}

おすすめ

転載: blog.csdn.net/qq_37959253/article/details/129463657