Reputation: 832
I am trying to convert an image stored in a folder in localhost for ex :
String imagePath = "http://localhost:8080/ABCD/profile_203.jpg"
to byte array, but i am getting this exception "javax.imageio.IIOException: Can't read input file!". When i give path of an image from some other location its getting converted to byte array.
String imagePath = "C:\\Users\\Vallabh.Lakade\\Desktop\\profilepic\\profile_203.jpg";
is working.This is my code.
try{
BufferedImage bufferedImage = ImageIO.read(new File(imagePath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", baos);
baos.flush();
byte[] imageInBytes = baos.toByteArray();
baos.close();
alertParams.put("imageInBytes", imageInBytes);
return new BaseVO(alertParams, Constants.STATUS_OK, Constants.STATUS_OK_MSG);
}
catch(Exception e){
return new BaseVO(alertParams, Constants.STATUS_ERROR, Constants.STATUS_ERROR_MSG);
}
Upvotes: 0
Views: 130
Reputation: 57421
The link is not file. Try to use URL instead
BufferedImage bufferedImage = ImageIO.read(new URL(imagePath));
Upvotes: 1