简体   繁体   English

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

[英]Reading file from FTP URL in Google App Engine, FileNotFoundException

I'm writing code to read a file from an FTP URL in order to parse it and store the data in Google App Engine's Datastore. 我正在编写代码以从FTP URL读取文件,以便对其进行解析并将数据存储在Google App Engine的数据存储区中。 I'm able to get the code working fine when reading test files hosted on my own web server, however when I try to read the data file I need I'm getting a FileNotFoundException. 读取托管在自己的Web服务器上的测试文件时,我能够使代码正常工作,但是,当我尝试读取数据文件时,我需要获取FileNotFoundException。

I'm able to use the same FTP URL in a browser to download the file, and can anonymously connect to the FTP URL in FileZilla, so access shouldn't be a problem, and the file is definitely there. 我可以在浏览器中使用相同的FTP URL来下载文件,并且可以匿名连接到FileZilla中的FTP URL,因此访问应该没有问题,并且文件肯定在那里。 It's a pretty big file, but I've tried to grab smaller files from the same FTP server with no luck either. 这是一个很大的文件,但我尝试从同一FTP服务器上抓取较小的文件,也没有运气。

Here's the code I have at the moment: 这是我目前的代码:

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;
}

At first I was getting an AccessControlException, but using a PrivilegedAction to open the stream seems to fix that. 刚开始我遇到了AccessControlException,但是使用PrivilegedAction打开流似乎可以解决此问题。

I don't have access to the server where the file is stored, so can't change anything there. 我无权访问存储文件的服务器,因此无法在此进行任何更改。

I think there is a restriction on the ports that can be connected to from App Engine and FTP (21) is not in the list, this maybe causing the issue. 我认为可以从App Engine连接到的端口受到限制,并且FTP(21)不在列表中,这可能会导致问题。 From the URL Fetch documentation ; 从URL Fetch文档 ;

An app can fetch a URL using HTTP (normal) or HTTPS (secure). 应用程序可以使用HTTP(常规)或HTTPS(安全)获取URL。 The URL specifies the scheme to use: http://... or https://... URL指定要使用的方案:http:// ...或https:// ...

The URL to be fetched can use any port number in the following ranges: 80-90, 440-450, 1024-65535. 要获取的URL可以使用以下范围内的任何端口号:80-90、440-450、1024-65535。 If the port is not mentioned in the URL, the port is implied by the scheme: http://... is port 80, https://... is port 443. 如果URL中未提及端口,则该方案暗示该端口:http:// ...是端口80,https:// ...是端口443。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM