mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2026-09-13 08:24:55 +00:00
fix: refresh ImageIO plugins before PDF conversion
This commit is contained in:
@@ -18,6 +18,7 @@ import org.springframework.stereotype.Component;
|
|||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -93,6 +94,8 @@ public class PdfToJpgService {
|
|||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
|
refreshImageIoPlugins();
|
||||||
|
|
||||||
int maxThreads = ConfigConstants.getPdfMaxThreads();
|
int maxThreads = ConfigConstants.getPdfMaxThreads();
|
||||||
// 使用固定大小的虚拟线程池
|
// 使用固定大小的虚拟线程池
|
||||||
this.virtualThreadExecutor = Executors.newFixedThreadPool(maxThreads,
|
this.virtualThreadExecutor = Executors.newFixedThreadPool(maxThreads,
|
||||||
@@ -104,6 +107,13 @@ public class PdfToJpgService {
|
|||||||
scheduleCacheCleanup();
|
scheduleCacheCleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void refreshImageIoPlugins() {
|
||||||
|
// ImageIO only scans once automatically. If another launcher or Java agent initializes
|
||||||
|
// it before Spring Boot installs its application class loader, nested JAR providers such
|
||||||
|
// as jbig2-imageio remain invisible until the application class path is scanned again.
|
||||||
|
ImageIO.scanForPlugins();
|
||||||
|
}
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
logger.info("开始关闭PDF转换服务...");
|
logger.info("开始关闭PDF转换服务...");
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package cn.keking.service;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.imageio.spi.IIORegistry;
|
||||||
|
import javax.imageio.spi.ImageReaderSpi;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class PdfToJpgServiceTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldRediscoverJbig2ReaderAfterInitialRegistryMiss() {
|
||||||
|
IIORegistry registry = IIORegistry.getDefaultInstance();
|
||||||
|
List<ImageReaderSpi> providers = findJbig2Providers(registry);
|
||||||
|
assertFalse(providers.isEmpty(), "jbig2-imageio must be present on the test class path");
|
||||||
|
|
||||||
|
try {
|
||||||
|
providers.forEach(registry::deregisterServiceProvider);
|
||||||
|
assertFalse(hasJbig2Reader());
|
||||||
|
|
||||||
|
PdfToJpgService.refreshImageIoPlugins();
|
||||||
|
|
||||||
|
assertTrue(hasJbig2Reader());
|
||||||
|
} finally {
|
||||||
|
providers.forEach(registry::registerServiceProvider);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<ImageReaderSpi> findJbig2Providers(IIORegistry registry) {
|
||||||
|
Iterator<ImageReaderSpi> providers = registry.getServiceProviders(
|
||||||
|
ImageReaderSpi.class,
|
||||||
|
provider -> Arrays.stream(((ImageReaderSpi) provider).getFormatNames())
|
||||||
|
.anyMatch("JBIG2"::equalsIgnoreCase),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
List<ImageReaderSpi> result = new ArrayList<>();
|
||||||
|
providers.forEachRemaining(result::add);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasJbig2Reader() {
|
||||||
|
return ImageIO.getImageReadersByFormatName("JBIG2").hasNext();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user