FTP上传文件名称中文乱码问题

这个问题昨天研究了一下午,以为解决了,今天早上突然发现其实并没有解决,所以花了一早上时间又查阅了相关资料后 现在才算彻底的解决。要注意:昨天我单纯的把filename转成new String(filename.getBytes("GBK"), "ISO-8859-1"),的确发现上传后中文不再乱码了,于是我以为我把这个问题解决了,但是后来发现如果ftp服务器中的设置就已经是utf-8的编码的话还是会继续报错的,所以我又上网找了些资料发现有个命令可以设置ftp支持UTF-8,就是这个ftpClient.sendCommand("OPTS UTF8", "ON"),所以就试了一下然后文件名字这样new String(filename.getBytes(LOCAL_CHARSET), SERVER_CHARSET),喏,这回乱码问题彻底解决了,下面是代码:

public class FTPFileUtil {
//此为FTP的配置文件信息
static Map<String, String> eip = UtilProperties.getPropretiesByKey();
//本地字符编码
static String LOCAL_CHARSET = "GBK";

// FTP协议里面,规定文件名编码为iso-8859-1
static String SERVER_CHARSET = "ISO-8859-1";

public static FTPClient ftpClient = null;

/***
* 初始化ftp服务器
* @author panfei
* @date 2018年7月18日
*/
public static void initFtpClient() {
ftpClient = new FTPClient();
try {
ICFLoggerUtils.info("connecting...ftp服务器:"+ eip.get("ftp.username") +":"+ eip.get("ftp.port"));
//连接ftp服务器
ftpClient.connect(eip.get("ftp.uploadpath"), Integer.valueOf(eip.get("ftp.port")));
//登录ftp服务器
ftpClient.login(eip.get("ftp.username"), eip.get("ftp.password"));
//是否成功登录服务器
int replyCode = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(replyCode)){
ICFLoggerUtils.info("connect failed...ftp服务器:"+eip.get("ftp.username")+":"+eip.get("ftp.port"));
}
ICFLoggerUtils.info("connect success...ftp服务器:"+eip.get("ftp.username")+":"+eip.get("ftp.port"));
// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {
LOCAL_CHARSET = "UTF-8";
}
ftpClient.setControlEncoding(LOCAL_CHARSET);
}catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}

/*****
* 上传ftp服务器
* @param pathname FTP服务器保存目录
* @param fis 流
* @param filename 要删除的文件名称
* @return
* @author panfei
* @date 2018年7月18日
*/
public static boolean createFile(String pathname, InputStream fis, String filename){
boolean flag = false;
try {
ICFLoggerUtils.info("开始上传文件");
initFtpClient();
ftpClient.makeDirectory(pathname);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(pathname);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
flag = ftpClient.storeFile(new String(filename.getBytes(LOCAL_CHARSET),
SERVER_CHARSET), fis);
if(flag){
ICFLoggerUtils.info("文件上传成功!");
}else{
ICFLoggerUtils.info("文件上传不成功!");
}
fis.close();
ftpClient.logout();
} catch (Exception e1) {
ICFLoggerUtils.info("上传文件失败");
e1.printStackTrace();
}finally {
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}

/***
* 删除文件
* @param pathname FTP服务器保存目录 *
* @param filename 要删除的文件名称 *
* @return
* @author panfei
* @date 2018年7月18日
*/
public static boolean deleteFile(String pathname, String filename){
boolean flag = false;
try {
ICFLoggerUtils.info("开始删除文件");
initFtpClient();
ftpClient.enterLocalPassiveMode();
//切换FTP目录
ftpClient.changeWorkingDirectory(pathname);
flag = ftpClient.deleteFile(new String(filename.getBytes(LOCAL_CHARSET),
SERVER_CHARSET));
ftpClient.logout();
if(flag){
ICFLoggerUtils.info("删除文件成功!");
}else{
ICFLoggerUtils.info("删除文件失败!");
}
} catch (Exception e) {
ICFLoggerUtils.info("删除文件失败");
e.printStackTrace();
} finally {
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
return flag;
}


public static void main(String[] args) throws IOException {
//InputStream inputStream = new FileInputStream("C:/Users/icfjk888/Workspaces/ICFInterfaceSrever-3.0/src/main/webapp/download/木兰.zip");
//createFile("tempDownload",inputStream,"木兰.zip");
//deleteFile("tempDownload", "木兰.zip");
}
}

原文链接:https://blog.csdn.net/pan_fei/article/details/81109635

猜你喜欢

转载自www.cnblogs.com/axin85/p/11741576.html