[英]Error while downloading a file from url
我正在嘗試從url讀取文件並使其成為File
類型。
下面是代碼
public File fileFromUrl(String str) throws IOException
{
File file = new File ("image.png");
URL url = new URL (str);
InputStream input = url.openConnection().getInputStream();
try {
OutputStream output = new FileOutputStream (file);
try {
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
return file;
}
但是我在OutputStream output = new FileOutputStream (file);
時遇到錯誤OutputStream output = new FileOutputStream (file);
請告訴我如何制作我的url
File
如果發布了您遇到的異常,這將很有幫助,但是我猜測您沒有嘗試在其中創建輸出文件的當前工作目錄的寫權限。
如果要查看它嘗試在哪里寫入文件,請將此診斷添加到程序中:
System.out.println(
"Absolute path of image.png: <" + file.getAbsolutePath( ) + ">"
);
精細。 而不是File file = new File ("image.png");
使用文件的絕對路徑。 像File file = new File ("<absolute-path-from-root-directory>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.