linux下的java通过ftp读取另一linux下的文件名出现中文乱码。
publicstaticvoidloadMyFile(StringregStr,Stringip,Stringusrname,Stringpwd,Integerm){co...
public static void loadMyFile(String regStr,String ip,String usrname,String pwd,Integer m){
connectMyServer(ip,usrname,pwd);
try {
FtpClient.changeWorkingDirectory(regStr);
String files[] = FtpClient.listNames(regStr);
if (files == null || files.length == 0)
System.out.println("没有文件!");
else {
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]); String filename=files[i].substring(regStr.length()+1,files[i].length());
System.out.println("文件名"+filename);
String remoteFileName=regStr+"/"+files[i];
BufferedOutputStream buffOut = null;
try {
buffOut = new BufferedOutputStream(new FileOutputStream("/doc"+files[i]));
FtpClient.retrieveFile(remoteFileName, buffOut);
} catch (Exception e) {
e.printStackTrace();
logger.debug("本地文件下载失败!", e);
} finally {
try {
if (buffOut != null)
buffOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
closeConnect();
}
}
/**
* 连接到服务器
*
* @return true 连接服务器成功,false 连接服务器失败
*/
public static boolean connectMyServer(String ip,String usrname,String pwd) {
boolean flag = true;
int reply;
try {
FtpClient = new FTPClient();
FtpClient.setControlEncoding("UTF-8");
FtpClient.configure(getFtpConfig());
FtpClient.connect(ip);
FtpClient.login(usrname, pwd);
FtpClient.setDefaultPort(Port);
System.out.print(FtpClient.getReplyString());
reply = FtpClient.getReplyCode();
FtpClient.setDataTimeout(120000);
if (!FTPReply.isPositiveCompletion(reply)) {
FtpClient.disconnect();
System.err.println("FTP server refused connection.");
logger.debug("FTP 服务拒绝连接!");
flag = false;
}
System.out.println(i);
} catch (SocketException e) {
flag = false;
e.printStackTrace();
System.err.println("登录ftp服务器【" + ip + "】失败,连接超时!");
} catch (IOException e) {
flag = false;
e.printStackTrace();
System.err.println("登录ftp服务器【" + ip + "】失败,FTP服务器无法打开!");
}
return flag;
} 展开
connectMyServer(ip,usrname,pwd);
try {
FtpClient.changeWorkingDirectory(regStr);
String files[] = FtpClient.listNames(regStr);
if (files == null || files.length == 0)
System.out.println("没有文件!");
else {
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]); String filename=files[i].substring(regStr.length()+1,files[i].length());
System.out.println("文件名"+filename);
String remoteFileName=regStr+"/"+files[i];
BufferedOutputStream buffOut = null;
try {
buffOut = new BufferedOutputStream(new FileOutputStream("/doc"+files[i]));
FtpClient.retrieveFile(remoteFileName, buffOut);
} catch (Exception e) {
e.printStackTrace();
logger.debug("本地文件下载失败!", e);
} finally {
try {
if (buffOut != null)
buffOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
closeConnect();
}
}
/**
* 连接到服务器
*
* @return true 连接服务器成功,false 连接服务器失败
*/
public static boolean connectMyServer(String ip,String usrname,String pwd) {
boolean flag = true;
int reply;
try {
FtpClient = new FTPClient();
FtpClient.setControlEncoding("UTF-8");
FtpClient.configure(getFtpConfig());
FtpClient.connect(ip);
FtpClient.login(usrname, pwd);
FtpClient.setDefaultPort(Port);
System.out.print(FtpClient.getReplyString());
reply = FtpClient.getReplyCode();
FtpClient.setDataTimeout(120000);
if (!FTPReply.isPositiveCompletion(reply)) {
FtpClient.disconnect();
System.err.println("FTP server refused connection.");
logger.debug("FTP 服务拒绝连接!");
flag = false;
}
System.out.println(i);
} catch (SocketException e) {
flag = false;
e.printStackTrace();
System.err.println("登录ftp服务器【" + ip + "】失败,连接超时!");
} catch (IOException e) {
flag = false;
e.printStackTrace();
System.err.println("登录ftp服务器【" + ip + "】失败,FTP服务器无法打开!");
}
return flag;
} 展开
1个回答
展开全部
需要转一下编码,你的java的class文家中是GBK的编码,对面linux下是iso8859-1编码
String fileNameTmp = new String(files[i].getBytes("iso-8859-1"), "GBK");//将从linux取得的文件名转换为GBK编码
String filename=fileNameTmp .substring(regStr.length()+1,fileNameTmp .length());
然后再把转完编码的文件名按你的要求进行截取
String fileNameTmp = new String(files[i].getBytes("iso-8859-1"), "GBK");//将从linux取得的文件名转换为GBK编码
String filename=fileNameTmp .substring(regStr.length()+1,fileNameTmp .length());
然后再把转完编码的文件名按你的要求进行截取
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询