服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - FTP服务器 - 完全解决FTP上传文件名称中文乱码问题

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

2023-10-13 10:18グッ!(๑•̀ㅂ•́)و非 FTP服务器

今天项目上加了个定时扫描本地文件路径下所有文件实现自动上传至ftp文件服务器的功能,经测试发现一旦上传中文名称的文件就会乱码或者文件上传不了。初步排查就是FTP服务器字符编码的问题。

说明

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

无效踩坑经历
  • 踩坑1(说明:不一定无效,但是我试了不行)

    本地文件名进行 (UTF-8 ,GBK) 和 ISO-8859-1 的转换

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");
    }
    
  • 踩坑2(说明:不一定无效,但是我试了不行)

    打开IIS服务器,然后找到FTP服务,选择高级配置,把允许utf8改成false

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

有效解决方法

思路:设置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 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()));
        }
    }
}

到此这篇关于完全解决FTP上传文件名称中文乱码问题的文章就介绍到这了,更多相关内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/qq_37959253/article/details/129463230

延伸 · 阅读

精彩推荐