我正在尝试从 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);
请告诉我我该怎么File
做url