1

im trying here at first check, if the file on URL exists and then if it exists download it. But it throws me

Exception in thread "main" sun.net.ftp.FtpProtocolException: Welcome message: 421 Too many connections (2) from this IP

If i know, i always close the connection, but anyway it crashes

private boolean exists(String URLName) throws MalformedURLException, IOException {
    boolean result = false;
        URL url = new URL(URLName);
        input = url.openStream();
        input.close();
        result = true;  
    return result;
}

private void downloadTheFile(String path, String name) throws MalformedURLException, IOException {
    input.close();
    input = new URL(path).openStream();
    try {
    OutputStream out = new FileOutputStream(name + ".pdf");
        byte buf[] = new byte[4096];
        for (int n = input.read(buf); n > 0; n = input.read(buf)) {
            out.write(buf, 0, n);
        }
    } finally {
        out.close();
        input.close();
    }
}

could anyone help me please?

4

2 回答 2

1

服务器发现您正在创建太多不同的连接,并立即将您启动。不要为每个文件创建单独的连接。

虽然我不知道您是如何调用下载函数的,但请考虑通过Apache Commons::net使用持久连接。

于 2013-08-16T12:20:25.637 回答
0

摆脱 exists() 方法。如果文件不存在,下载方法将失败。仅此一项就会将您的连接减少一半。

于 2013-08-16T12:38:35.077 回答