JSP老旧项目迁徙过来的代码,天生海报,会读取图片,读取字体文件,绘制图片,会天生大量临时文件,内存走漏。
方案一,服务器定时删除temp临时文件夹
方案二,图片、字体改用静态类读取文件,并且开释相关资源文件。
导致缘故原由
- @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;
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |