有人说修改MAVEN配置 这个是治标不治本呀
从1.7开始这个类就彻底被移除了
其实JPEGCodec 的一些方法可以用其他方式代替!
private static ByteArrayInputStream convertImageToStream(BufferedImage bufferedImage) {
ByteArrayInputStream byteArrayInputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder(byteArrayOutputStream);
try {
imageEncoder.encode(bufferedImage);
byte[] bytes = byteArrayOutputStream.toByteArray();
byteArrayInputStream = new ByteArrayInputStream(bytes);
} catch (Exception e) {
Logger.getLogger(SecurityImage.class.getName()).log(Level.SEVERE, null, e);
}
return byteArrayInputStream;
}
可以写成
private static ByteArrayInputStream convertImageToStream(BufferedImage bufferedImage) {
ByteArrayInputStream byteArrayInputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder(byteArrayOutputStream);
try {
ImageIO.write(bufferedImage, "jpeg" , byteArrayOutputStream);
bufferedImage.flush();
byte[] bytes = byteArrayOutputStream.toByteArray();
byteArrayInputStream = new ByteArrayInputStream(bytes);
} catch (Exception e) {
Logger.getLogger(SecurityImage.class.getName()).log(Level.SEVERE, null, e);
}finally{
if (byteArrayOutputStream!=null) {
try {
byteArrayOutputStream.close();
} catch (IOException ex) {
Logger.getLogger(SecurityImage.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (byteArrayInputStream!=null) {
try {
byteArrayInputStream.close();
} catch (IOException ex) {
Logger.getLogger(SecurityImage.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return byteArrayInputStream;
}