从Google App Engine中的FTP URL读取文件,FileNotFoundException

时间:2012-06-07 15:21:49

标签: java google-app-engine ftp

我正在编写代码来从FTP URL读取文件,以便解析它并将数据存储在Google App Engine的数据存储区中。在阅读我自己的Web服务器上托管的测试文件时,我能够正常运行代码,但是当我尝试读取数据文件时,我需要一个FileNotFoundException。

我可以在浏览器中使用相同的FTP URL来下载文件,并且可以匿名连接到FileZilla中的FTP URL,因此访问不应该是一个问题,文件肯定存在。这是一个非常大的文件,但我试图从同一个FTP服务器抓取较小的文件,但也没有运气。

这是我目前的代码:

public void doGet(HttpServletRequest req,
        HttpServletResponse resp) throws IOException, ServletException {

    // works with a URL to my own server & a test.zip, but not this one
    final URL url = new URL(
        "ftp://gisftp.metc.state.mn.us/google_transit.zip");

    // without the privileged action, I get an AccessControlException
    ZipInputStream zin = AccessController.doPrivileged(
        new PrivilegedAction<ZipInputStream>() {
            public ZipInputStream run() {
                return getZipStream(url);
            }
        }
    );

    ZipEntry zipentry = zin.getNextEntry();

    // processing files here

    zin.close();
}

// but using the privileged method, we get a FileNotFoundException
public ZipInputStream getZipStream(URL url) {
    ZipInputStream zipin = null;
    try {
        zipin = new ZipInputStream(url.openStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return zipin;
}

起初我得到一个AccessControlException,但使用PrivilegedAction打开流似乎解决了这个问题。

我无法访问存储文件的服务器,因此无法更改任何内容。

1 个答案:

答案 0 :(得分:1)

我认为可以从App Engine连接的端口有限制,而FTP(21)不在列表中,这可能会导致问题。 From the URL Fetch documentation;

  

应用可以使用HTTP(普通)或HTTPS(安全)获取网址。 URL指定要使用的方案:http:// ...或https:// ...

     

要获取的URL可以使用以下范围内的任何端口号:80-90,440-450,1024-65535。如果URL中未提及该端口,则该方案暗示该端口:http:// ...是端口80,https:// ...是端口443。

相关问题