设置FTP客户端为被动模式

zoutuo 2009-10-07 12:41:33
我所使用的FTP服务器不支持主动连接方式,所以我的程序需要将连接方式设置成为被动。我已经设置成为被动模式了,但是无法上传文件。下面是我的代码,请大虾过目:
连接服务器并设置为被动模式:

public void connectServer(String ip, int port, String user, String password, String path, JTextArea textArea) {
try {
ftpClient = new FtpClient();
setConnectTimeOut(5000);
ftpClient.openServer(ip, port);
ftpClient.login(user, password);
// textArea.append(ReadFiles.getDate() + " 登陆成功!\n");
// textArea.setCaretPosition(textArea.getText().length());
System.out.println("登陆成功!");
if (path.length() != 0){
ftpClient.cd(path);
}
ftpClient.binary();
ftpClient.sendServer("PASV");
} catch (IOException ex) {
// textArea.append(ReadFiles.getDate() + " 登陆失败!"+ex+"\n");
// textArea.setCaretPosition(textArea.getText().length());
System.out.println("登陆失败!");
System.out.println(ex);
ex.printStackTrace();
resetFtpClient();
connectServer(ip, port, user, password, path, textArea);
}
}

上传文件:

public void upload(String path, String localfilename, String remotefilename, JTextArea textArea) {
changeDirs(path, textArea);
this.localfilename = localfilename; // c:/1.txt
this.remotefilename = remotefilename;// 1.txt
System.out.println("UPLOADING...");
textArea.append(ReadFiles.getDate() + " 正在上传 [" + remotefilename + "]...\n");
textArea.setCaretPosition(textArea.getText().length());
try {
TelnetOutputStream out = ftpClient.put(this.remotefilename);
File file_in = new File(this.localfilename);
FileInputStream in = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
}
textArea.append(ReadFiles.getDate() + " 上传文件 [" + remotefilename + "]成功!\n");
textArea.setCaretPosition(textArea.getText().length());
in.close();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("ERROR!");
textArea.append(ex + "\n");
textArea.append(ReadFiles.getDate() + " 上传文件失败!\n");
textArea.setCaretPosition(textArea.getText().length());

resetFtpClient();
connectServer(host, port, username, password, path, textArea);
upload(path, localfilename, remotefilename, textArea);
}
}


以下是异常:

sun.net.ftp.FtpProtocolException: REIN:502 REIN not implemented.


将FTP连接设置为被动模式,谢谢!!!

	void resetFtpClient(){
try {
if(ftpClient.serverIsOpen()){
ftpClient.reInit();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}

进入异常后,执行上面这个方法时报错:
ftpClient.reInit();

谢谢各位!
...全文
354 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zoutuo 2009-10-08
  • 打赏
  • 举报
回复
这怎么没人吱声呢??????连续3次回复之后就不能再回复了,不会的帮我顶一下啊,谢谢各位!!
zoutuo 2009-10-07
  • 打赏
  • 举报
回复
......
zoutuo 2009-10-07
  • 打赏
  • 举报
回复
晕,是分太少?没人回答?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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