问题 :通过java连接ftp服务器上传文件时,文件中文名称出现乱码问题?
环境:在linux上搭建的FTP服务
原来代码:
/**
* 上传文件
*/
public static void uploadFile(File file, String fileName, String bizInstGuid,String ftpHost,String ftpPort,String ftpUserName,String ftpPassword,String ftpPath){
if (StringUtils.isEmpty(ftpPath)) {
ftpPath = bizInstGuid;
} else {
ftpPath = ftpPath + "/" + bizInstGuid;
}
FTPClient ftpClient=null;
try {
ftpClient=getFTPClient(ftpHost,ftpPort,ftpUserName,ftpPassword);
//逐级创建目录
String[] split = ftpPath.split("/");
for (String str : split) {
if(StringUtils.isBlank(str)) {
continue;
}
if (!ftpClient.changeWorkingDirectory(str)) {
boolean makeDirectory = ftpClient.makeDirectory(str);
boolean changeWorkingDirectory = ftpClient.changeWorkingDirectory(str);
if (!makeDirectory || !changeWorkingDirectory) {
throw new RuntimeException(str+"目录创建失败");
}
}
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
fileName=new String(fileName.getBytes("GBK"),"iso-8859-1");
InputStream inputStream = new FileInputStream(file);
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
修改后的代码:
package com.gisquest.platform.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import com.gisquest.platform.common.config.Global;
public class DynamicFtpUtil {
/**
* 获取FTPClient对象
* @return
*/
public static FTPClient getFTPClient(String ftpHost,String ftpPort,String ftpUserName,String ftpPassword) {
FTPClient ftpClient = null;
try {
//创建一个ftp客户端
ftpClient = new FTPClient();
// 连接FTP服务器
ftpClient.connect(ftpHost, Integer.parseInt(ftpPort));
// 登陆FTP服务器
ftpClient.login(ftpUserName, ftpPassword);
//是否成功登录服务器
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
throw new RuntimeException("未连接到FTP,用户名或密码错误。");
}
} catch (SocketException e) {
throw new RuntimeException("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
throw new RuntimeException("FTP的端口错误,请正确配置。");
}
return ftpClient;
}
/**
* 上传文件
*/
public static void uploadFile(File file, String fileName, String bizInstGuid,String ftpHost,String ftpPort,String ftpUserName,String ftpPassword,String ftpPath){
if (StringUtils.isEmpty(ftpPath)) {
ftpPath = bizInstGuid;
} else {
ftpPath = ftpPath + "/" + bizInstGuid;
}
FTPClient ftpClient=null;
try {
ftpClient=getFTPClient(ftpHost,ftpPort,ftpUserName,ftpPassword);
//逐级创建目录
String[] split = ftpPath.split("/");
for (String str : split) {
if(StringUtils.isBlank(str)) {
continue;
}
if (!ftpClient.changeWorkingDirectory(str)) {
boolean makeDirectory = ftpClient.makeDirectory(str);
boolean changeWorkingDirectory = ftpClient.changeWorkingDirectory(str);
if (!makeDirectory || !changeWorkingDirectory) {
throw new RuntimeException(str+"目录创建失败");
}
}
}
//处理上传文件后文件名称为中文乱码
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");
InputStream inputStream = new FileInputStream(file);
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
/**
* 下载文件
* @return
*/
public static byte[] downloadFile(String fileName, String bizInstGuid,String ftpHost,String ftpPort,String ftpUserName,String ftpPassword,String ftpPath) {
if (StringUtils.isEmpty(ftpPath)) {
ftpPath = bizInstGuid;
} else {
ftpPath = ftpPath + "/" + bizInstGuid;
}
FTPClient ftpClient = null;
try {
ftpClient =getFTPClient(ftpHost,ftpPort,ftpUserName,ftpPassword);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
fileName=new String(fileName.getBytes("GBK"),"iso-8859-1");
InputStream inputStream = ftpClient.retrieveFileStream(fileName);
byte filedata[] =null;
if (inputStream != null) {
ByteArrayOutputStream bytestream =null;
try {
bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = inputStream.read()) != -1) {
bytestream.write(ch);
}
filedata = bytestream.toByteArray();
} finally {
if (bytestream != null) {
bytestream.close();
}
}
}
inputStream.close();
ftpClient.completePendingCommand();
ftpClient.logout();
return filedata;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
/**
* 删除文件
*/
public static void deleteFile(String fileName, String bizInstGuid,String ftpHost,String ftpPort,String ftpUserName,String ftpPassword) {
String ftpPath = Global.getConfig("ftpPath");//ftp服务保存地址
if (StringUtils.isEmpty(ftpPath)) {
ftpPath = bizInstGuid;
} else {
ftpPath = ftpPath + "/" + bizInstGuid;
}
FTPClient ftpClient = null;
try {
ftpClient = getFTPClient(ftpHost,ftpPort,ftpUserName,ftpPassword);
ftpClient.changeWorkingDirectory(ftpPath);
fileName=new String(fileName.getBytes("GBK"),"iso-8859-1");
ftpClient.dele(fileName);
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
查看了网上提供的一些思路将linux中环境编码设置成zh_CN.GBK
LINUX 查看环境编码设置
echo $LANG
echo $LC_ALL
linux 设置环境编码设置
export LC_ALL="zh_CN.GBK"
export LANG="zh_CN.GBK"