我正在编写代码以从 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 打开流似乎可以解决这个问题。
我无权访问存储文件的服务器,因此无法更改任何内容。