1:使用org.apache.commons.net.ftp.FTPClient
2:调整后的上传文件java代码:
public boolean uploadTxtFile(String filename, String r,
String tempftpDirectory) {
boolean success = false;
InputStream is = null;
try {
String LOCAL_CHARSET="GBK";
if (FTPReply.isPositiveCompletion(this.ftpClient.sendCommand("OPTS UTF8", "ON"))) {
LOCAL_CHARSET = "UTF-8";
}
this.ftpClient.setControlEncoding(LOCAL_CHARSET);
// 上传文件时,文件名称需要做编码转换
String name = new String(filename.getBytes(LOCAL_CHARSET),"ISO-8859-1");
// System.err.println("setControlEncoding编码:"+LOCAL_CHARSET+"-----转码后的文件名称:"+fileName);
// 1.输入流
is = new ByteArrayInputStream(r.getBytes("utf-8"));
this.ftpClient.changeWorkingDirectory(tempftpDirectory);// 改变工作路径
success = this.ftpClient.storeFile(name, is);
if (success == true) {
return success;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
3:如图文件名称乱码
4:将文件从windows搭建的ftp服务器取下来存放到linux下(windows下搭建服务器使用的是Gene6 FTP Server)
5:执行linux命令:convmv -r -f utf8 -t iso88591 * --notest --nosmart && convmv -r -f gbk -t utf8 * --notest --nosmart
6:这个命令需要安装 yum install convmv
7:结果如图: