读取图片时抛异常,解决方法:删掉本地图片,重新跑测试,正常;
异常信息:
javax.imageio.IIOException: Bogus marker length
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method) ~[na:1.8.0_66]
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1236) ~[na:1.8.0_66]
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1039) ~[na:1.8.0_66]
at javax.imageio.ImageIO.read(ImageIO.java:1448) ~[na:1.8.0_66]
at javax.imageio.ImageIO.read(ImageIO.java:1352) ~[na:1.8.0_66]
伪代码:
//文件名后缀长度
int typeLen = imagePath.substring(imagePath.indexOf(".") + 1).length();
//文件类型
String type = imagePath.substring(imagePath.length() - typeLen);
BufferedImage bm = ImageIO.read(
new BufferedInputStream(
new FileInputStream(
new File(imagePath))));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bm, type, bos);