ftpclient 遇到的一些问题

1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据

 

public static boolean ftpLogin(String server,int port,String userName,String userPassword,FTPClient ftpClient){
if(ftpClient==null){
ftpClient= new FTPClient();
}
boolean isLogin = false;
if(ftpClient!=null && ftpClient.isConnected()){
isLogin=true;
return isLogin;
}
ftpClient.setControlEncoding("GBK");
try {
if (port > 0) {
ftpClient.connect(server, port);
} else {
ftpClient.connect(server);
}
// FTP服务器连接回答
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
ToolUtils.logger.error("登录FTP服务失败!");
return isLogin;
}
ftpClient.login(userName, userPassword);
// 设置传输协议
ftpClient.enterLocalPassiveMode();    //应该是这个,没有主动打开端口接收数据
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ToolUtils.logger.warn("恭喜" + userName + "成功登陆FTP服务器");
isLogin = true;
} catch (Exception e) {
e.printStackTrace();
ToolUtils.logger.error(userName + "登录FTP服务失败!" + e.getMessage());
}
ftpClient.setBufferSize(1024 * 2);
ftpClient.setDataTimeout(30 * 1000);
return isLogin;
}

 

2.中文乱码

ftpClient.setControlEncoding("GBK");

 

3.ftpClient.storeFile(new String(filename.getBytes("UTF-8"),"iso-8859-1"),is) ;无法写入内容

3.1 ftpClient.changeWorkingDirectory(filepath);//必须写上一次目录 列:/a/a.txt  只写/a  或者/a/ 不然的话他把文件创建在根目录

3.2 服务端防火墙拦截

3.3 ftp文件没有赋予用户写入权限

 

4.中文目录无法进去,修改内容

ftpClient.storeFile(URLDecoder.decode(filename, "iso-8859-1"),is);//可以解决,一些人使用new string 进行转码,不能成功。具体原因我也不知道,求大神回答。

 

posted on 2016-09-05 17:10  sui008  阅读(1285)  评论(0编辑  收藏  举报

导航