• 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

    Add JPEG encoder/decoder support to installed OpenJDK

  • 相关阅读:
    flash不保存在IE缓存中
    pv 3d
    ASP.NET优化
    根据文件名判断文件扩展名 和 根据流真正判断文件类型的关键函数
    as3.0
    编码
    AS3中Event的target和currentTarget
    显示用户IP来源的代码
    启用IIS的Gzip压缩功能
    Css之多个子元素水平平均分布——flex布局法
  • 原文地址:https://www.cnblogs.com/shiyun32/p/15918613.html
Copyright © 2020-2023  润新知