我需要一个非常简单的函数,它允许我通过FTP读取文件的前1k个字节。我想在MATLAB中使用它来读取第一行,并根据一些参数,只下载我最终真正需要的文件。我在网上发现了一些例子,不幸的是不工作。在这里,我提出了示例代码,我试图下载一个单独的文件(我使用的是Apache库)。读取文件的第一个字节
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("data.site.org");
// filename to be downloaded.
String filename = "filename.Z";
fos = new FileOutputStream(filename);
// Download file from FTP server
InputStream stream = client.retrieveFileStream("/pub/obs/2008/021/ab120210.08d.Z");
byte[] b = new byte[1024];
stream.read(b);
fos.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
错误在流中返回空。我知道我以错误的方式传递文件夹名称,但我无法理解我该怎么做。我尝试了很多方法。
我也与URL的Java类尝试作为:
URL url;
url = new URL("ftp://data.site.org/pub/obs/2008/021/ab120210.08d.Z");
URLConnection con = url.openConnection();
BufferedInputStream in =
new BufferedInputStream(con.getInputStream());
FileOutputStream out =
new FileOutputStream("C:\\filename.Z");
int i;
byte[] bytesIn = new byte[1024];
if ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn);
}
out.close();
in.close();
,但它给当我关闭InputStream的一个错误!
我绝对卡住了。有些评论会非常有用!
欢迎计算器!没有必要为标题添加标签,这里有一个标签系统。请阅读http://meta.stackexchange.com/q/19190/147072了解更多信息。此外,最后不需要添加“谢谢”或您的姓名,每个人都感谢您的帮助,并且您的姓名会显示在您创建的每个问题和答案的右下角的角色表中。 – Patrick