我正试图从ftp服务器下载一个文件。filezilla已成功下载该文件。但是java应用程序给出错误“425控制和数据连接的ip地址不匹配”。
禁用地址匹配验证不起作用。我在用apachecommonsnet图书馆。远程服务器运行在nat后面,本地服务器运行在代理后面。
我怎样才能解决这个问题?
package javaappftptest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.commons.net.util.TrustManagerUtils;
public class JavaAppFtpTest {
public static void main(String[] args) {
String server = "<ftpsHostIP>"; //hostname or hostIP
int port = 990; //port ftps
String user = "<ftpLogin>";
String pass = "<ftpPassword>";
String proxyHost = "<proxyIP>"; //Local proxy params
int proxyPort = <proxyPort>;
Type typeProxy = Type.HTTP;
int reply;
FTPSClient ftpClient = new FTPSClient(true); //create the ftps client (implicit ftps)
try {
SocketAddress addr = new InetSocketAddress(proxyHost,proxyPort); //set proxy params to the client
Proxy proxy = new Proxy(typeProxy,addr);
ftpClient.setProxy(proxy);
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out),true)); //Add protocol listener for logging of commands
ftpClient.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
ftpClient.setControlKeepAliveTimeout(10000L);
ftpClient.setDataTimeout(30000);
System.out.println("Try connect to host " + server + " port " + port);
ftpClient.connect(server,port); // connecting to ftps server
ftpClient.execPBSZ(0); // set buffer size (unlimit)
ftpClient.execPROT("P"); // set protected mode P
reply = ftpClient.getReplyCode();
System.out.println("ReplyCode = " + reply);
ftpClient.login(user, pass); // login to server
reply = ftpClient.getReplyCode();
System.out.println("ReplyCode = " + reply);
String remoteDir = "/201014";
ftpClient.changeWorkingDirectory(remoteDir); // change remote dir on server
reply = ftpClient.getReplyCode();
System.out.println("ReplyCode = " + reply);
ftpClient.printWorkingDirectory();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // set file type (image = binary type)
reply = ftpClient.getReplyCode();
System.out.println("ReplyCode = " + reply);
ftpClient.enterLocalPassiveMode(); // set passive mode
String remoteFile1 = "img22103106251_3.jpg";
File downloadFile1 = new File("img22103106251_3.jpg");
ftpClient.setRemoteVerificationEnabled(false); //disable verification of matching command and data addresses
reply = ftpClient.getReplyCode();
System.out.println("ReplyCode = " + reply);
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1); // download file to local directory
if (!success) {
System.out.println("ERROR. File has NOT been downloaded");
}
reply = ftpClient.getReplyCode();
System.out.println("ReplyCode = " + reply);
outputStream1.close();
if (success) {
System.out.println("File has been downloaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
输出:
Try connect to host <ftpHostIP> port 990
220 %Unauthorized access prohibited%
PBSZ 0
200 PBSZ=0
PROT P
200 Protection level set to P
ReplyCode = 200
USER*******
331 Password required for <ftpLogin>
PASS*******
230 Logged on
ReplyCode = 230
CWD /201014
250 CWD successful. "/201014" is current directory.
ReplyCode = 250
PWD
257 "/201014" is current directory.
TYPE I
200 Type set to I
ReplyCode = 200
ReplyCode = 200
PASV
227 Entering Passive Mode (xxx,xx,xx,xx,xxx,xxx) <-IP matches to the ftpHostIP!
RETR img22103106251_3.jpg
425 Rejected data connection for transfer of "/201014/img22103106251_3.jpg", IP addresses of control and data connection do not match
ERROR. File has NOT been downloaded
ReplyCode = 425
QUIT
221 Goodbye
暂无答案!
目前还没有任何答案,快来回答吧!