我需要一個非常簡單的函數,它允許我通過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