导出图片报错:java.lang.ArrayIndexOutOfBoundsException: 0
程序员文章站
2022-07-15 10:18:42
...
导出图片报错:java.lang.ArrayIndexOutOfBoundsException: 0
解决办法:
import cn.afterturn.easypoi.cache.ImageCache;
import cn.afterturn.easypoi.cache.manager.POICacheManager;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.poi.util.IOUtils;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
@Component
public class ExcelListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
LoadingCache<String, byte[]> loadingCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.DAYS)
.maximumSize(2000).build(new CacheLoader<String, byte[]>() {
@Override
public byte[] load(String imagePath) throws Exception {
InputStream is = POICacheManager.getFile(imagePath);
BufferedImage bufferImg = ImageIO.read(is);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
try {
ImageIO.write(bufferImg,
imagePath.substring(imagePath.lastIndexOf(".")+1),
byteArrayOut);
return byteArrayOut.toByteArray();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(byteArrayOut);
}
}
});
ImageCache.setLoadingCache(loadingCache);
}
}
推荐阅读
-
mysql 导出数据报错: row must be in range 0-65535
-
php5.6 上传图片error代码为6 或者 报错“PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0”的解决办法
-
导出图片报错:java.lang.ArrayIndexOutOfBoundsException: 0
-
Typecho 截取文章第一张图片报错 Notice: Undefined offset: 0
-
php5.6 上传图片error代码为6 或者 报错“PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0”的解决办法