检查是否带有"ftp"的文件URL使用java存在 [英] Check if file with "ftp" url exists using java

查看:55
本文介绍了检查是否带有"ftp"的文件URL使用java存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问任何人,我如何检查仅FTP协议的URL上是否存在文件?我正在使用此代码:

please, could anyone tell me, how can i check if file exists on URL where is only FTP protocol? Im using this code:

   public boolean exists(String URLName) throws IOException {
        input = null;
        boolean result = false;
        try {
            input = new URL(URLName).openStream();
            System.out.println("SUCCESS");
            result = true;
        } catch (Exception e) {
            System.out.println("FAIL");
        } finally {
            if (input != null) {
                input.close();
                input = null;
            }
        }
        return result;
    }

当我发送到那里一两个以上时,它不起作用,只是说

It doesnt work when i send there more then one or two, it just sais

    sun.net.ftp.FtpProtocolException: Welcome message: 421 Too many connections (2) from this IP

        at sun.net.ftp.FtpClient.openServer(FtpClient.java:490)
        at sun.net.ftp.FtpClient.openServer(FtpClient.java:475)


at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:270)
    at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:352)
    at java.net.URL.openStream(URL.java:1010)
    at bibparsing.PDFImage.exists(PDFImage.java:168)
    at bibparsing.PDFImage.main(PDFImage.java:189)

当协议为HTTP时,它的效果很好.我的意思是像这样的地址:

It works great when the protocol is HTTP. I mean adresses like:

ftp://cmp.felk.cvut.cz/pub/cmp/articles/chum/Chum-TR-2001-27.pdf ftp://cmp.felk.cvut.cz/pub/cmp/articles/martinec/Kamberov-ISVC2006.pdf 还有类似的东西

推荐答案

这里的问题是此方法不是线程安全的;如果两个线程同时使用此方法,则一个线程可以覆盖名为 input 的实例变量,从而导致另一个线程不关闭其打开的连接(并且不关闭任何内容,或关闭另一个线程打开的连接).

The problem here is that this method isn't thread safe; if two threads use this method simultaneously one can overwrite the instance variable named input, causing the other thread to not closing the connection it opened (and closing either nothing, or the connection opened by the other thread).

通过将 input 变量设置为局部变量,可以轻松解决此问题:

This is easily fixed, by making the input variable local:

InputStream input=null;

代码样式:,您可以在知道方法后立即返回结果.初学者通常先声明变量,然后执行逻辑并在方法末尾返回结果.您可以通过

Code style: within a method, you can return the result as soon as you know it. Beginners often declare the variables first, then execute the logic and return the result at the end of the method. You can save a lot of code and complexity by

  • 尽可能晚地声明变量 (当您第一次需要它们时)
  • 声明尽可能少的变量(可读性始终是添加变量的一个很好的理由,但变量越少意味着复杂度越低)
  • 在知道结果后立即返回 (减少代码的路径,从而降低复杂性)
  • declaring variables as late as possible (when you first need them)
  • declaring as few variables as necessary (readability is always a good reason to add variables, but less variables means less complexity)
  • returning as soon as you know the result (reducing paths through your code, and thus reducing complexity)

该代码可以简单地写为:

The code can be simply written as:

public static boolean exists (String urlName) throws IOException {
    try {
        new URL(urlName).openStream().close();
        return true;
    } catch (IOException e) {
        return false;
    }
}

这篇关于检查是否带有"ftp"的文件URL使用java存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆