I have tried some functions to convert url image to byte buffer but it doesn't work. It shows an IOException error
String imagePath = "https://www.clearias.com/up/UPSC-Civil-Services-Mains-Exam-2018-Timetable-V1.png";
Try 1:
URL u = new URL(imagePath);
int contentLength = u.openConnection().getContentLength();
InputStream openStream = u.openStream();
byte[] binaryData = new byte[contentLength];
openStream.read(binaryData);
ByteBuffer imageBytes = ByteBuffer.wrap(openStream);
This shows error when i wrap openStream to ByteBuffer
"The method wrap(byte[]) in the type ByteBuffer is not applicable for the arguments (InputStream)"
Try 2:
URL url = new URL(imagePath);
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (InputStream inputStream = url.openStream()) {
int n = 0;
byte[] buffer = new byte[1024];
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
}
}
byte[] img = output.toByteArray();
ByteBuffer imageBytes = ByteBuffer.wrap(img);
I have also tried this function but it shows this error:
java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.clearias.com/up/UPSC-Civil-Services-Mains-Exam-2018-Timetable-V1.png
Try 3: And another is
byte[] img = Base64.encodeBase64(IOUtils.toByteArray((new URL(imagePath)).openStream()), true);
This line also gives me error