tomcat temp临时文件不清空,占用硬盘,jdk字体内存泄漏

JSP老旧项目迁移过来的代码,生成海报,会读取图片,读取字体文件,绘制图片,会生成大量临时文件,内存泄漏。
方案一,服务器定时删除temp临时文件夹
方案二,图片、字体改用静态类读取文件,并且释放相关资源文件。


导致原因

java 复制代码
package com.huida.train.utils;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Component
public class ResourceUtil {
    // 使用volatile保证多线程环境下的可见性,防止指令重排
    private static volatile BufferedImage activityShareBannerImage;
    private static volatile Font sourceHanSansFont;
    private static volatile BufferedImage backgroundImage;
    private static volatile Font rdFont;

    /**
     * 方正小标宋字体
     */
    private static volatile Font fzxbsjwFont;

    /**
     * 方正黑体
     */
    private static volatile Font htFont;

    /**
     * 仿宋
     */
    private static volatile Font fsFont;

    private static volatile BufferedImage bottomTextImg;


    private ResourceUtil() {
        // 私有构造函数,防止外部实例化
    }
    public static BufferedImage getBottomTextImg() {
        if (bottomTextImg == null) {
            synchronized (ResourceUtil.class) {
                if (bottomTextImg == null) {
                    try {
                        ClassPathResource resource = new ClassPathResource("static/qr/bottomText.png");
                        bottomTextImg = ImageIO.read(resource.getInputStream());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return bottomTextImg;
    }

    public static Font getFsFont() {
        if (fsFont == null) {
            synchronized (ResourceUtil.class) {
                if (fsFont == null) {
                    try {
                        ClassPathResource fsFontResource = new ClassPathResource("static/font/fs_gbk.TTF");
                        fsFont = Font.createFont(Font.TRUETYPE_FONT, fsFontResource.getInputStream());
                    } catch (IOException | FontFormatException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return fsFont;
    }
    public static Font getHtFont() {
        if (htFont == null) {
            synchronized (ResourceUtil.class) {
                if (htFont == null) {
                    try {
                        ClassPathResource fsFontResource = new ClassPathResource("static/font/ht.TTF");
                        htFont = Font.createFont(Font.TRUETYPE_FONT, fsFontResource.getInputStream());
                    } catch (IOException | FontFormatException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return htFont;
    }

    public static Font getFzxbsjwFont() {
        if (fzxbsjwFont == null) {
            synchronized (ResourceUtil.class) {
                if (fzxbsjwFont == null) {
                    try {
                        ClassPathResource fsFontResource = new ClassPathResource("static/font/FZXBSJW.TTF");
                        fzxbsjwFont = Font.createFont(Font.TRUETYPE_FONT, fsFontResource.getInputStream());
                    } catch (IOException | FontFormatException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return fzxbsjwFont;
    }
    public static Font getSimsunFont() {
        if (rdFont == null) {
            synchronized (ResourceUtil.class) {
                if (rdFont == null) {
                    try {
                        ClassPathResource fsFontResource = new ClassPathResource("static/font/simsun.ttf");
                        rdFont = Font.createFont(Font.TRUETYPE_FONT, fsFontResource.getInputStream());
                    } catch (IOException | FontFormatException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return rdFont;
    }

    public static BufferedImage getBackgroundImage() {
        if (backgroundImage == null) {
            synchronized (ResourceUtil.class) {
                if (backgroundImage == null) {
                    try {
                        ClassPathResource resource = new ClassPathResource("static/qr/backgroundImage.png");
                        backgroundImage = ImageIO.read(resource.getInputStream());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return backgroundImage;
    }

    public static BufferedImage getActivityShareBannerImage() {
        if (activityShareBannerImage == null) {
            synchronized (ResourceUtil.class) {
                if (activityShareBannerImage == null) {
                    try {
                        ClassPathResource resource = new ClassPathResource("static/qr/activity_share_banner.png");
                        activityShareBannerImage = ImageIO.read(resource.getInputStream());
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return activityShareBannerImage;
    }

    public static Font getSourceHanSansFont() {
        if (sourceHanSansFont == null) {
            synchronized (ResourceUtil.class) {
                if (sourceHanSansFont == null) {
                    try {
                        ClassPathResource fsFontResource = new ClassPathResource("static/font/SourceHanSans-Regular.otf");
                        sourceHanSansFont = Font.createFont(Font.TRUETYPE_FONT, fsFontResource.getInputStream());
                    } catch (IOException | FontFormatException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return sourceHanSansFont;
    }
}
java 复制代码
 @Override
    public String getStrainingShareCode2(Map<String, String> map) throws IOException, FontFormatException {
        String trainingCourseId = map.get("trainingCourseId");
        TrainingCourse trainingCoursePo = trainingCourseMapper.select(trainingCourseId);
        if (trainingCoursePo == null) {
            throw new TipUserException("活动-培训已被删除或不可见");
        }
        BufferedImage activityShareBannerImage = ResourceUtil.getActivityShareBannerImage();
        // 开启画图
        Graphics2D graphics = activityShareBannerImage.createGraphics();
        //字体样式
        Font font = ResourceUtil.getSourceHanSansFont().deriveFont(Font.BOLD, 27);
        //字体颜色
        float[] color = Color.RGBtoHSB(58, 58, 58, null);
        graphics.setColor(Color.getHSBColor(color[0], color[1], color[2]));
        graphics.setFont(font);
        String address = "";
        //地点
        if (trainingCoursePo.getAddress().length() >= 16) {
            address = trainingCoursePo.getAddress().subSequence(0, 14) + "...";
        } else {
            address = trainingCoursePo.getAddress();
        }
        drawWrapString(graphics, address, 50, 220, activityShareBannerImage.getWidth() - 100, null, null);
        //时间
        drawWrapString(graphics, DateUtils.dateTime(trainingCoursePo.getStartTime()) + " ~ " + DateUtils.dateTime(trainingCoursePo.getEndTime()), 50, 275, activityShareBannerImage.getWidth() - 100, null, null);
        //报名人数
        Long enrollNum = trainingCoursePo.getEnrollNum();
        if (ObjectUtils.isNull(enrollNum)) {
            enrollNum = 0L;
        }
        drawWrapString(graphics, enrollNum.toString() + "人报名", 50, 330, activityShareBannerImage.getWidth() - 100, null, null);
//        String base64 = ImgUtil.toBase64DataUri(activityShareBannerImage, "png");
//        return base64;
        // 指定文件路径
        String filePath = HuiDaConfig.getTrainUploadPath() + "/shareCode/" + trainingCoursePo.getRecordId() + "/" + trainingCoursePo.getRecordId() + "_" + enrollNum + ".png";
        File outputfile = new File(filePath);
        if (!outputfile.exists()) {
            String deleteFilePath = HuiDaConfig.getTrainUploadPath() + "/shareCode/" + trainingCoursePo.getRecordId();
            File deleteFileFile = new File(deleteFilePath);
            FileUtils.deleteDirectory(deleteFileFile);
            File parentDir = outputfile.getParentFile();
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
            ImageIO.write(activityShareBannerImage, "png", outputfile);
        }
        activityShareBannerImage.flush();
        graphics.dispose();
        return filePath;
    }
相关推荐
Yan.love1 分钟前
【MyBatis 核心工作机制】注解式开发与动态代理原理
java·mybatis
A22741 分钟前
Redis——双写一致性
java·redis·缓存
No regret.11 分钟前
JVM内存模型、垃圾回收机制及简单调优方式
java·开发语言·jvm
爱学习的小羊啊1 小时前
从零开始掌握Spring MVC:深入解析@Controller与@RequestMapping注解的使用
java·spring·mvc
小李不想输啦4 小时前
什么是微服务、微服务如何实现Eureka,网关是什么,nacos是什么
java·spring boot·微服务·eureka·架构
张铁铁是个小胖子4 小时前
微服务学习
java·学习·微服务
ggs_and_ddu4 小时前
Android--java实现手机亮度控制
android·java·智能手机
敲代码娶不了六花6 小时前
jsp | servlet | spring forEach读取不了对象List
java·spring·servlet·tomcat·list·jsp
Yhame.6 小时前
深入理解 Java 中的 ArrayList 和 List:泛型与动态数组
java·开发语言
是小崔啊7 小时前
开源轮子 - EasyExcel02(深入实践)
java·开源·excel