0

我正在编写代码以从 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 打开流似乎可以解决这个问题。

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

4

1 回答 1

1

我认为可以从 App Engine 连接的端口受到限制,并且 FTP (21) 不在列表中,这可能会导致问题。来自 URL Fetch 文档

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

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

于 2012-06-08T13:50:07.477 回答