本文使用FTPClient对FTP进行文件操作,FTPClient工具需要添加Maven依赖。

     <!-- commons-net FTP工具类--><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.6</version></dependency>

在测试文件上传过程中,发现中文名称的文件上传到FTP服务器乱码。

问题解决的思路是设置上传文件名称的编码格式为UTF-8,那么怎么设置呢?为了一劳永逸的方式解决文件上传下载均不会乱码,在FTP登录的时候设置字符编码,这样就不用担心乱码问题了。下面看代码

 public static FTPClient connectFTP(String ip, int port, String uname, String pwd) throws IOException {FTPClient ftpClient = new FTPClient();// 优先设置编码格式,避免中文乱码ftpClient.setAutodetectUTF8(true);ftpClient.setCharset(CharsetUtil.UTF_8);ftpClient.setControlEncoding(CharsetUtil.UTF_8.name());ftpClient.connect(ip, port);boolean login = ftpClient.login(uname, pwd);if (!login) {throw new BusinessException("ftp连接失败,请检查配置");}ftpClient.enterLocalPassiveMode();ftpClient.setFileType(FTP.BINARY_FILE_TYPE);ftpClient.setBufferSize(DEFAULT_BUFFER_SIZE);return ftpClient;}

其中CharsetUtil使用的依赖包是netty的依赖,需要在Maven添加netty依赖。

        <!-- netty-common --><dependency><groupId>io.netty</groupId><artifactId>netty-common</artifactId><version>4.1.77.Final</version></dependency>

下面提供一个简单的FTP工具类

import xxx.xxx.xxx.BusinessException;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;/*** @author * @date 2022/8/2 15:53* @description*/
@Slf4j
public class FtpUtils {private static final int DEFAULT_BUFFER_SIZE = 4096;/*** 上传文件** @param input      输入流* @param uploadPath 保存路径* @param fileName   文件名(包含格式后缀)* @param ftpClient  FTP客户端连接* @throws IOException*/public static void upload(InputStream input, String uploadPath, String fileName, FTPClient ftpClient) {try {// 判断路径是否存在,不存在则新建checkFTPPath(ftpClient, uploadPath);// 保存文件到路径ftpClient.storeFile(fileName, input);log.info("您的文件【{}】上传成功。", fileName);} catch (IOException e) {log.error("FTP上传文件时出错", e);throw new BusinessException("sys.file.fail");} finally {// 关闭连接IOUtils.closeQuietly(input);closeFTP(ftpClient);}}/*** 上传文件** @param input      输入流* @param uploadPath 保存路径* @param fileName   文件名(包含格式后缀)* @param ip* @param port* @param uname* @param pwd* @throws IOException*/public static void upload(InputStream input, String uploadPath, String fileName, String ip, int port, String uname, String pwd) {FTPClient ftpClient = new FTPClient();try {// 获取FTP客户端ftpClient = connectFTP(ip, port, uname, pwd);// 判断路径是否存在,不存在则新建checkFTPPath(ftpClient, uploadPath);// 保存文件到路径ftpClient.storeFile(fileName, input);log.info("您的文件【{}】上传成功。", fileName);} catch (IOException e) {log.error("FTP上传文件时出错", e);throw new BusinessException("sys.file.fail");} finally {// 关闭连接IOUtils.closeQuietly(input);closeFTP(ftpClient);}}/*** 删除文件** @param path      文件地址* @param ftpClient FTP客户端* @return* @throws IOException*/public static Boolean deleteFile(String path, FTPClient ftpClient) {Boolean flag = false;try {//删除if (ftpClient != null) {flag = ftpClient.deleteFile(path);log.info("您的文件【{}】删除成功。", path);}} catch (IOException e) {log.error("FTP删除文件时出错", e);throw new BusinessException("sys.file.delete.fail");} finally {closeFTP(ftpClient);}return flag;}/*** 删除文件** @param path  文件地址* @param ip* @param port* @param uname* @param pwd* @return* @throws IOException*/public static Boolean deleteFile(String path, String ip, int port, String uname, String pwd) {Boolean flag = false;FTPClient ftpClient = new FTPClient();try {// 获取FTP客户端ftpClient = connectFTP(ip, port, uname, pwd);//删除if (ftpClient != null) {flag = ftpClient.deleteFile(path);log.info("您的文件【{}】删除成功。", path);}} catch (IOException e) {log.error("FTP删除文件时出错", e);throw new BusinessException("sys.file.delete.fail");} finally {closeFTP(ftpClient);}return flag;}/*** 删除目录** @param path      目录地址* @param ftpClient FTP客户端* @return* @throws IOException*/public static Boolean deleteDirectory(String path, FTPClient ftpClient) {Boolean flag = false;try {//删除if (ftpClient != null) {flag = ftpClient.removeDirectory(path);log.info("您的目录【{}】删除成功。", path);}} catch (IOException e) {log.error("FTP删除目录时出错", e);throw new BusinessException("sys.file.delete.fail");} finally {closeFTP(ftpClient);}return flag;}/*** 删除目录** @param path  目录地址* @param ip* @param port* @param uname* @param pwd* @return* @throws IOException*/public static Boolean deleteDirectory(String path, String ip, int port, String uname, String pwd) {Boolean flag = false;FTPClient ftpClient = new FTPClient();try {// 获取FTP客户端ftpClient = connectFTP(ip, port, uname, pwd);//删除if (ftpClient != null) {flag = ftpClient.removeDirectory(path);log.info("您的目录【{}】删除成功。", path);}} catch (IOException e) {log.error("FTP删除目录时出错", e);throw new BusinessException("sys.file.delete.fail");} finally {closeFTP(ftpClient);}return flag;}/*** 从ftp上下载** @param output     输出流* @param remoteFile 文件路径* @param ftpClient  FTP客户端*/public static void download(OutputStream output, String remoteFile, FTPClient ftpClient) {try {ftpClient.enterLocalPassiveMode();ftpClient.retrieveFile(remoteFile, output);output.flush();log.info("您的文件【{}】下载成功。", remoteFile);} catch (Exception e) {log.error("下载文件出错:", e);} finally {IOUtils.closeQuietly(output);closeFTP(ftpClient);}}/*** 从ftp上下载** @param output     输出流* @param remoteFile 文件路径* @param ip* @param port* @param uname* @param pwd*/public static void download(OutputStream output, String remoteFile, String ip, int port, String uname, String pwd) {FTPClient ftpClient = new FTPClient();try {// 获取FTP客户端ftpClient = connectFTP(ip, port, uname, pwd);ftpClient.enterLocalPassiveMode();ftpClient.retrieveFile(remoteFile, output);output.flush();log.info("您的文件【{}】下载成功。", remoteFile);} catch (Exception e) {log.error("下载文件出错:", e);} finally {IOUtils.closeQuietly(output);closeFTP(ftpClient);}}/*** 连接FTP服务器** @param ip* @param port* @param uname* @param pwd* @return* @throws IOException*/public static FTPClient connectFTP(String ip, int port, String uname, String pwd) throws IOException {FTPClient ftpClient = new FTPClient();// 优先设置编码格式,避免中文乱码ftpClient.setAutodetectUTF8(true);ftpClient.setCharset(CharsetUtil.UTF_8);ftpClient.setControlEncoding(CharsetUtil.UTF_8.name());ftpClient.connect(ip, port);boolean login = ftpClient.login(uname, pwd);if (!login) {throw new BusinessException("ftp连接失败,请检查配置");}ftpClient.enterLocalPassiveMode();ftpClient.setFileType(FTP.BINARY_FILE_TYPE);ftpClient.setBufferSize(DEFAULT_BUFFER_SIZE);return ftpClient;}/*** 关闭连接** @param ftpClient*/public static void closeFTP(FTPClient ftpClient) {if (ftpClient != null) {try {ftpClient.disconnect();} catch (IOException e) {log.error("关闭FTP连接出错", e);throw new BusinessException("sys.file.fail");}}}/*** 校验路径是否存在,不存在则新建** @param ftpClient* @param uploadPath* @throws IOException*/private static void checkFTPPath(FTPClient ftpClient, String uploadPath) throws IOException {//判断路径是否存在,不存在则新建if (uploadPath != null) {String[] paths = uploadPath.split("/");StringBuffer tempPath = new StringBuffer();for (String path : paths) {if ((path != null) && (!"".equals(path.trim()))) {String temp = tempPath.append("/").append(path.trim()).toString();if ((!ftpClient.changeWorkingDirectory(temp)) && (ftpClient.makeDirectory(temp))) {ftpClient.changeWorkingDirectory(temp);}}}}}}

FTP上传文件 名称中文乱码问题相关推荐

  1. 解决ServletFileUpload上传文件时,获取上传文件名出现中文乱码问题

    解决ServletFileUpload上传文件时,获取上传文件名出现中文乱码问题 在我们使用ServletFileUpload上传文件时,我们通常会获取其上传的文件名,然而当文件名包含中文时,便可能出 ...

  2. java ftp上传文件相关代码梳理

    java实现ftp文件上传 1.ftp文件上传步骤 1)连接ftp 2)检查工作目录是否存在 3)检查是否上传成功 4)完成任务后,断开ftp 2.具体如下,直接贴上核心代码: A)导入核心pom依赖 ...

  3. php 上传文件名乱码,php上传文件时文件名乱码怎么办

    php上传文件时文件名乱码的解决方法:首先在脚本头部添加[header("Content-type: text/html; charset=utf-8");]:然后利用iconv( ...

  4. ftp 文件夹 上传到服务器,ftp上传文件夹到服务器 远程路径

    ftp上传文件夹到服务器 远程路径 内容精选 换一换 WinSCP工具可以实现在本地与远程计算机之间安全地复制文件.与使用FTP上传代码相比,通过 WinSCP 可以直接使用服务器账户密码访问服务器, ...

  5. QT FTP上传文件

    QT FTP上传文件 两台电脑通过网线建立本地连接,保证网关在同一段: 服务器端打开ftp: 客户端网页测试远程访问: 客户端cmd测试远程访问: 客户端程序测试远程访问. 两台电脑通过网线建立本地连 ...

  6. linux通过ftp自动上传文件到服务器,Linux系统通过FTP上传文件到云服务器

    如何通过FTP将文件上传到腾讯云Linux云服务器?上一篇小编给大家介绍了通过Winscp将文件上传到云服务器的方法,今天小编为大家介绍过FTP将文件上传到腾讯云Linux云服务器的方法,用户需要使用 ...

  7. 关于用批处理写ftp上传文件

    利用批处理FTP上传文件 是益阳市局的一位朋友介绍的,不用编程就可实现文件FTP传输. 建一个批处理文件hntqyb.bat,内容为: @echo off ftp -s:sendtqyb.txt 17 ...

  8. 使用ftp上传文件到Unix系统注意事项

    使用ftp上传文件到Unix系统注意事项 在使用FTP客户端上传文件到Unix系统时,应该注意将传输模式设置为二进制(Binary)传输模式,否则有可能造成传输到Unix系统的文件与实际文件大小不一致 ...

  9. Linux - xshell上传文件报错乱码

    xshell上传文件报错乱码,解决方法 rz -be 回车 下载sz  filename 转载于:https://www.cnblogs.com/RzCong/p/8600899.html

最新文章

  1. 【Redis】14.Redis高级数据类型Bitmaps、HyperLogLog、GEO
  2. 生产中的12种容器镜像扫描最佳实践
  3. java命令框编译代码的方式_在命令行模式下如何编译运行Java代码
  4. 64. 合并排序数组 II
  5. [No000014B]Office-PPT设置默认打开视图
  6. adb连接雷电模拟器修改hosts
  7. 科技信息它们叫嚣:没有我们,谈什么iPhone8!
  8. 分解质因数FZU - 1075
  9. Greenplum数据库巡检报告
  10. 【Unity3D】资源文件 ③ ( Unity 资源包简介 | 导出 Unity 资源包 | 导出资源包的包含依赖选项 | 导入 Unity 资源包 | Unity 资源商店 )
  11. 科技热点周刊|Linux 30 周年、Horizon Workroom 发布、Humanoid Robot、元宇宙
  12. 介绍一款LaTeX编辑器——LyX
  13. C语言报错:error: expected ‘while’ at end of input } ^
  14. monkey测试linux设备,关于使用Monkey运行脚本测试
  15. 客户机操作系统已禁用cpu_CPU硬件辅助虚拟化技术
  16. ESP8266-Arduino编程实例-BMA400加速度传感器驱动
  17. 付海棠 - 一个农民的亿万传奇(2015年7月14日)
  18. linux 快速处理文本文件,Linux 下三种高效的文件处理技巧
  19. NNDL 实验五 前馈神经网络(1)二分类任务
  20. 纽约时报悼念新冠去世患者页面是怎么做出来的?

热门文章

  1. 手机版个人中心html,手机版个人中心页面.html
  2. Django项目之CRM客户关系管理——表结构的设计及MySQL的连接
  3. html中div滚动条设置,DIV滚动条属性及样式设置方式
  4. 变阻感器测量位移的计算机流程图,位移传感器结构框图_位移传感器的工作原理 - 全文...
  5. iPhone名词解释
  6. 监控宝 mysql_使用监控宝监控你的Linux服务器(附图)
  7. 《挪威的森林》 感想
  8. springCloud Feign调用报错 Parameter 6 of constructor in xxxxxxxx required a bean of type ‘xxxxxx‘ that
  9. C++如何写日志文件(含源码 可直接套用)
  10. from PIL import Image不能导入Image