简体   繁体   中英

cannot convert image to byte array

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);
}

The link is not file. Try to use URL instead

BufferedImage bufferedImage = ImageIO.read(new URL(imagePath));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM