1

私たちのネットワークチームは、プロキシサーバーを介してサードパーティのクライアントに接続することのみを許可しています.
apache commons netのFTPSクライアントにプロキシサーバーを追加する方法はありますか?
無理ならやり方を教えていただけないでしょうか。

ちなみに、これは会社のネットワークの外で動作しているコードです

String server = "ftp.xxxx.com";
    String username = "username";
    String password = "password";
    String remoteFile = "xmlSR.xml";
    String localFile = "c:/downloadedfile.xml";
    String protocol = "TLS"; // TLS / SSL
    int timeoutInMillis = 3000;
    boolean isImpicit = false;

    FTPSClient client = new FTPSClient(protocol, isImpicit);
    client.enterLocalActiveMode();
    client.setRemoteVerificationEnabled(false);
    client.setActivePortRange(50000, 50200);
    client.setDataTimeout(timeoutInMillis);
    client.addProtocolCommandListener(new PrintCommandListener(
            new PrintWriter(System.out)));
    client.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());

    try {
        int reply;

        client.connect(server);

        client.login(username, password);
        client.setFileType(FTP.BINARY_FILE_TYPE);

        client.execPBSZ(0);
        client.execPROT("P");

        System.out.println("Connected to " + server + ".");

        reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }

        client.listFiles();

        boolean retrieved = client.retrieveFile(remoteFile,
                new FileOutputStream(localFile));

    } catch (Exception e) {

        if (client.isConnected()) {
            try {
                client.disconnect();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        return;
    } finally {
        System.out.println("# client disconnected");
        client.disconnect();
    }

proxyHost と proxyPort のシステム プロパティを追加しようとしても、

System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxyHost", "yyyy.com");
System.setProperty("ftp.proxyPort", "80");
System.setProperty("ftp.proxyHost", "yyyy.com");
System.setProperty("socksProxyPort", "80");
System.setProperty("socksProxyHost", "yyyy.com");

エラーメッセージ

Could not connect to server.
java.net.UnknownHostException: ftp.xxxx.com
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1202)
at java.net.InetAddress.getAllByName0(InetAddress.java:1153)
at java.net.InetAddress.getAllByName(InetAddress.java:1083)
at java.net.InetAddress.getAllByName(InetAddress.java:1019)
at java.net.InetAddress.getByName(InetAddress.java:969)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)# client disconnected

at org.apache.commons.net.SocketClient.connect(SocketClient.java:285)
at com.ti.itg.peit.bom.TestingApache.main(TestingApache.java:44)

どうもありがとうございました。

ジェラルド

4

2 に答える 2

0

その可能性..以下のリンクを参照してください....

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/ProxySelector.java

Apacheの共通ライブラリを使用してプロキシサーバーを使用してFTPにアクセスするにRemoteVerificationEnabledは、FTPClientオブジェクトの作成後にをfalseに設定してください。

例えば:

FTPClient fc = new FTPClient();  
fc.setRemoteVerificationEnabled(false);  

Java 1.5以降用のクラスを使用できますjava.net.Proxy。これは、接続ごとにプロキシを設定または設定解除するために使用されます

を使用してjava.net.ProxySelector、各接続のプロキシを決定します。

于 2012-07-09T10:02:38.560 に答える