瀏覽代碼

!70 修正PDF转图片,内存无法回收导致的OOM
Merge pull request !70 from SawyerYong/N/A

陈精华 2 年之前
父節點
當前提交
daa081d46d

+ 2 - 0
server/src/main/java/cn/keking/service/FileHandlerService.java

@@ -4,6 +4,7 @@ import cn.keking.config.ConfigConstants;
 import cn.keking.model.FileAttribute;
 import cn.keking.model.FileType;
 import cn.keking.service.cache.CacheService;
+import cn.keking.service.cache.NotResourceCache;
 import cn.keking.utils.KkFileUtils;
 import cn.keking.utils.WebUtils;
 import com.aspose.cad.CodePages;
@@ -198,6 +199,7 @@ public class FileHandlerService {
                 return null;
             }
             PDDocument doc = PDDocument.load(pdfFile);
+            doc.setResourceCache(new NotResourceCache());
             int pageCount = doc.getNumberOfPages();
             PDFRenderer pdfRenderer = new PDFRenderer(doc);
 

+ 20 - 0
server/src/main/java/cn/keking/service/cache/NotResourceCache.java

@@ -0,0 +1,20 @@
+package cn.keking.service.cache;
+
+import org.apache.pdfbox.cos.COSObject;
+import org.apache.pdfbox.pdmodel.DefaultResourceCache;
+import org.apache.pdfbox.pdmodel.graphics.PDXObject;
+
+import java.io.IOException;
+
+/**
+ * @author: Sawyer.Yong
+ * @since: 2023/02/18 14:45
+ * 解决图片 SoftReference 导致内存无法被回收导致的OOM, 详见 https://issues.apache.org/jira/browse/PDFBOX-3700
+ */
+public class NotResourceCache extends DefaultResourceCache {
+
+    @Override
+    public void put(COSObject indirect, PDXObject xobject) throws IOException {
+        // do nothing
+    }
+}