I am trying to read a file from url and making it a File Type.
Below is the code
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;
}
However I am experiencing error at OutputStream output = new FileOutputStream (file);
Kindly tell me how can I make File of my url
Fine. Instead of
File file = new File ("image.png");use absolute path for the file. LikeFile file = new File ("<absolute-path-from-root-directory>");