Host attempting data connection 192.168.90.151 is not same as server 192.168.90.

a7908095 2012-03-23 08:45:14
下面代码出现IO异常
Host attempting data connection 192.168.90.151 is not same as server 192.168.90.24

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class ClientFtpApp {

private String ip = "192.168.90.24";
private int port = 21;
private String username = "jiang";
private String password = "jiang";
byte[] image={};
/**
* @param args
*/
public static void main(String[] args) {
ClientFtpApp ftp = new ClientFtpApp();
ftp.downLoadFile("d:\\", "./test/", "41.jpg");
}
public static FileInputStream fileToStream(String filePath) {
File file = new File(filePath);
FileInputStream in = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return in;
}

/**根据图片流转化为byte
* @param InputStream 图片流
* @return byte[]
*/
private static byte[] readStream(InputStream inputStream) throws Throwable {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);

}
inputStream.close();
outStream.close();
return outStream.toByteArray();
}
public byte[] downLoadFile(String localPath, String remotePath, String fileName) {
FTPFile[] files = null;
FTPClient client = null;
try {
int reply;
if (client == null) {
try {
client = new FTPClient();
client.setControlEncoding("utf-8");
client.setDefaultPort(port);
client.connect(ip, port);
client.login(username, password);

reply = client.getReplyCode();
client.setDataTimeout(120000);
client.setFileType(client.BINARY_FILE_TYPE);
client.setFileTransferMode(client.STREAM_TRANSFER_MODE);
System.out.println(client.getStatus());
System.out.println(ip + " 连接成功");
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
System.out.println(ip + " connect refused!");
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println(ip + " 连接超时!");
} catch (IOException e) {
e.printStackTrace();
System.out.println(ip + " 服务器无法打开!");
}
}

client.changeWorkingDirectory(remotePath);

String path = localPath + File.separator + fileName;
File localFile = new File(localPath + File.separator + fileName);

FileOutputStream fos = new FileOutputStream(localFile);
client.retrieveFile(fileName, fos);
// InputStream ins = client.retrieveFileStream(fileName);
// try {
// image = readStream(ins);
// } catch (Throwable e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println(image);
fos.close();


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (client != null) {
try {
client.logout();
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return image;
}
}
...全文
1296 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
a7908095 2012-08-14
  • 打赏
  • 举报
回复
这个需要设置一个参数,这个参数允许启用/禁用远程主机连接验证。它将帮助当获得主机的ip地址数据连接尝试不是一样的服务器”的问题。与remoteverification禁用验证=“否”。默认值是“真”。

学习者 2012-04-20
  • 打赏
  • 举报
回复
我今天也遇到这个问题了,请问楼主是怎么解决的呢?

50,544

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧