完全解决FTP上传文件名称中文乱码问题

7 篇文章 0 订阅

完全解决FTP上传文件名称中文乱码问题

说明

今天项目上加了个定时扫描本地文件路径下所有文件实现自动上传至ftp文件服务器的功能,经测试发现一旦上传中文名称的文件就会乱码或者文件上传不了。初步排查就是FTP服务器字符编码的问题。在网上查了很多资料都没有效果。在这里讲一下我踩过的坑,以及分享我能上传成功的样例。

无效踩坑经历


public static String encodingUTF8(String path) throws UnsupportedEncodingException {
        return new String(path.getBytes("UTF-8"), "ISO-8859-1");
    }
    
public static String encodingGBK(String path) throws UnsupportedEncodingException {
        return new String(path.getBytes("GBK"), "ISO-8859-1");
    }
    

请添加图片描述

有效解决方法

思路:设置ftp支持UTF-8, ftpClient.sendCommand(“OPTS UTF8”, “ON”),核心代码如下

//链接至ftp服务器,设置编码格式 
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样例

引用工具组件: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()));
        }
    }
}

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值