java 定时自动上传本地文件至ftp服务器

7 篇文章 0 订阅
该Java代码实现了一个线程,用于定期扫描本地文件夹并将文件上传到FTP服务器。它利用了Hutool库进行FTP操作,包括设置UTF-8编码、被动模式,以及上传文件后的本地文件删除。如果上传失败,程序会捕获并打印异常信息。
摘要由CSDN通过智能技术生成

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()));
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值