解决办法:
- 连接ftp之后发送使用UTF8编码指令
ftpClient.sendCommand("OPTS UTF8", "ON");
- 文件上传之前对文件名进行转码
//转码为UTF8
private static String encodingPath(String path) {
return org.apache.commons.lang3.StringUtils.toEncodedString(path.replaceAll("//", "/").getBytes(),StandardCharsets.UTF_8);
//文件上传
private static boolean uploadFile(String remoteFilePath, File file) throws Exception {
FTPClient ftpClient = null ;
try (FileInputStream fileInputStream = new FileInputStream(file)) {
ftpClient = getFtpClient();
// 上传之前转码
return ftpClient.storeFile(encodingPath(remoteFilePath),fileInputStream);
} finally {
disconnect(ftpClient);
}
}