openjdk ImageIO.write()时出现Invalid argument to native writeImage
问题:在本地开发时使用的是oracle jdk8,没有问题,但是生产上面使用的是openjdk-8u252-b09,生成图片时一直报Invalid argument to native writeImage
javax.imageio.IIOException: Invalid argument to native writeImage
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1067)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:363)
at javax.imageio.ImageWriter.write(ImageWriter.java:615)
at javax.imageio.ImageIO.doWrite(ImageIO.java:1622)
at javax.imageio.ImageIO.write(ImageIO.java:1548)
代码(报错diam):
public String exportImg(List<Node> nodes, String piclocation, Font font){ //1.jpg模板路径 Resource resource = new ClassPathResource(piclocation); String path = ""; String fileName = ""; BufferedImage buffImg = null; InputStream inputStream = null; try { inputStream = resource.getInputStream(); buffImg = ImageIO.read(inputStream); //得到画笔对象 Graphics g = buffImg.getGraphics(); //设置颜色。 g.setColor(Color.BLACK); Graphics2D tip = buffImg.createGraphics(); tip.setColor(Color.BLACK); font = new Font("宋体", Font.PLAIN, 14); tip.setFont(font); picDraw(nodes, tip); g.dispose(); path = System.getProperty("java.io.tmpdir") + File.separator + File.separator; File file = new File(path); file.mkdirs(); fileName = UUID.randomUUID() + ".jpg"; ImageIO.write(buffImg, "jpg", new File(path + fileName)); } catch (IOException e) { e.printStackTrace(); } finally { try { //关闭流 if (inputStream != null) { inputStream.close(); } } catch (Exception finalExcp) { return finalExcp.getMessage(); } } return path + fileName; }
解决:
将上面红色的代码改为:
ImageIO.write(buffImg, "png", new File(path + fileName));
参考:
Using ImageIO.write() to create a JPEG creates a 0 byte file