How to calculate java BufferedImage filesize

后端 未结 6 1466
闹比i
闹比i 2021-01-04 04:41

I have a servlet based application that is serving images from files stored locally. I have added logic that will allow the application to load the image file to a Buffered

6条回答
  •  生来不讨喜
    2021-01-04 05:00

        BufferedImage img = = new BufferedImage(500, 300, BufferedImage.TYPE_INT_RGB);
    
        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        ImageIO.write(img, "png", tmp);
        tmp.close();
        Integer contentLength = tmp.size();
    
        response.setContentType("image/png");
        response.setHeader("Content-Length",contentLength.toString());
        OutputStream out = response.getOutputStream();
        out.write(tmp.toByteArray());
        out.close();
    

提交回复
热议问题