Browse Source

图片使用统一缓存处理

4.1.3.A
jueyue 9 years ago
parent
commit
7ceb71c32f
8 changed files with 158 additions and 30 deletions
  1. +70
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/cache/ImageCache.java
  2. +5
    -4
      easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/POICacheManager.java
  3. +4
    -22
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java
  4. +3
    -2
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java
  5. +21
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/excel/package-info.java
  6. +4
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/entity/PdfExportParams.java
  7. +30
    -2
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java
  8. +21
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/word/package-info.java

+ 70
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/cache/ImageCache.java View File

@@ -0,0 +1,70 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jeecgframework.poi.cache;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.poi.util.IOUtils;
import org.jeecgframework.poi.cache.manager.POICacheManager;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/**
* 图片缓存处理
* @author JueYue
* @date 2016年1月8日 下午4:16:32
*/
public class ImageCache {
private static LoadingCache<String, byte[]> loadingCache;
static {
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.indexOf(".") + 1, imagePath.length()),
byteArrayOut);
return byteArrayOut.toByteArray();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(byteArrayOut);
}
}
});
}
public static byte[] getImage(String imagePath) {
try {
return loadingCache.get(imagePath);
} catch (Exception e) {
return null;
}
}
}

+ 5
- 4
easypoi-base/src/main/java/org/jeecgframework/poi/cache/manager/POICacheManager.java View File

@@ -38,16 +38,17 @@ import com.google.common.cache.LoadingCache;
*/
public final class POICacheManager {
private static final Logger LOGGER = LoggerFactory.getLogger(POICacheManager.class);
private static final Logger LOGGER = LoggerFactory
.getLogger(POICacheManager.class);
private static LoadingCache<String, byte[]> loadingCache;
private static IFileLoader fileLoder;
private static IFileLoader fileLoder;
private static ThreadLocal<IFileLoader> LOCAL_FILELOADER = new ThreadLocal<IFileLoader>();
private static ThreadLocal<IFileLoader> LOCAL_FILELOADER = new ThreadLocal<IFileLoader>();
static {
loadingCache = CacheBuilder.newBuilder().expireAfterWrite(7, TimeUnit.DAYS).maximumSize(50)
loadingCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).maximumSize(50)
.build(new CacheLoader<String, byte[]>() {
@Override
public byte[] load(String url) throws Exception {


+ 4
- 22
easypoi-base/src/main/java/org/jeecgframework/poi/excel/export/base/ExcelExportBase.java View File

@@ -15,10 +15,6 @@
*/
package org.jeecgframework.poi.excel.export.base;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.HashMap;
@@ -26,8 +22,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
@@ -42,6 +36,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.jeecgframework.poi.cache.ImageCache;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.entity.vo.PoiBaseConstants;
@@ -170,22 +165,9 @@ public abstract class ExcelExportBase extends ExportBase {
return;
}
if (entity.getExportImageType() == 1) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg;
try {
String path = PoiPublicUtil.getWebRootPath(imagePath);
path = path.replace("WEB-INF/classes/", "");
path = path.replace("file:/", "");
bufferImg = ImageIO.read(new File(path));
ImageIO.write(bufferImg,
imagePath.substring(imagePath.indexOf(".") + 1, imagePath.length()),
byteArrayOut);
byte[] value = byteArrayOut.toByteArray();
patriarch.createPicture(anchor,
row.getSheet().getWorkbook().addPicture(value, getImageType(value)));
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
byte[] value = ImageCache.getImage(imagePath);
patriarch.createPicture(anchor,
row.getSheet().getWorkbook().addPicture(value, getImageType(value)));
} else {
byte[] value = (byte[]) (entity.getMethods() != null
? getFieldBySomeMethod(entity.getMethods(), obj)


+ 3
- 2
easypoi-base/src/main/java/org/jeecgframework/poi/excel/imports/CellValueServer.java View File

@@ -173,7 +173,7 @@ public class CellValueServer {
* @param cell
* @param titleString
*/
public Object getValue(IExcelDataHandler dataHanlder, Object object, Cell cell,
public Object getValue(IExcelDataHandler<?> dataHanlder, Object object, Cell cell,
Map<String, ExcelImportEntity> excelParams,
String titleString) throws Exception {
ExcelImportEntity entity = excelParams.get(titleString);
@@ -202,7 +202,7 @@ public class CellValueServer {
* @param titleString
* @return
*/
public Object getValue(IExcelDataHandler dataHanlder, Object object,
public Object getValue(IExcelDataHandler<?> dataHanlder, Object object,
SaxReadCellEntity cellEntity, Map<String, ExcelImportEntity> excelParams,
String titleString) {
ExcelImportEntity entity = excelParams.get(titleString);
@@ -304,6 +304,7 @@ public class CellValueServer {
* @param titleString
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object hanlderValue(IExcelDataHandler dataHanlder, Object object, Object result,
String titleString) {
if (dataHanlder == null || dataHanlder.getNeedHandlerFields() == null


+ 21
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/excel/package-info.java View File

@@ -0,0 +1,21 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Excel模块,支持2003,2007的导入导出,模板的导出,流的导出(大数据量)
* @author JueYue
* @date 2014年6月20日 上午12:08:09
*/
package org.jeecgframework.poi.excel;

+ 4
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/entity/PdfExportParams.java View File

@@ -63,6 +63,10 @@ public class PdfExportParams extends ExcelBaseParams {
}
public PdfExportParams(String title) {
this.title = title;
}
public PdfExportParams(String title, String secondTitle) {
this.title = title;
this.secondTitle = secondTitle;


+ 30
- 2
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java View File

@@ -15,14 +15,17 @@
*/
package org.jeecgframework.poi.pdf.export;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.poi.cache.ImageCache;
import org.jeecgframework.poi.excel.annotation.ExcelTarget;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.export.base.ExportBase;
@@ -33,9 +36,11 @@ import org.jeecgframework.poi.util.PoiPublicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
@@ -144,7 +149,8 @@ public class PdfExportServer extends ExportBase {
createStringCell(table, value == null ? "" : value.toString(), entity,
rowHeight, 1, maxHeight);
} else {
createImageCell(table, value == null ? "" : value.toString(), entity, rowHeight,
1, maxHeight);
}
}
}
@@ -167,7 +173,8 @@ public class PdfExportServer extends ExportBase {
if (entity.getType() == 1) {
createStringCell(table, value == null ? "" : value.toString(), entity, rowHeight);
} else {
createImageCell(table, value == null ? "" : value.toString(), entity, rowHeight, 1,
1);
}
}
}
@@ -299,4 +306,25 @@ public class PdfExportServer extends ExportBase {
return iCell;
}
private PdfPCell createImageCell(PdfPTable table, String text, ExcelExportEntity entity,
int rowHeight, int rowSpan, int colSpan) {
try {
Image image = Image.getInstance(ImageCache.getImage(text));
PdfPCell iCell = new PdfPCell(image);
styler.setCellStyler(iCell, entity, text);
iCell.setFixedHeight((int) (rowHeight * 2.5));
table.addCell(iCell);
return iCell;
} catch (BadElementException e) {
LOGGER.error(e.getMessage(), e);
} catch (MalformedURLException e) {
LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
return new PdfPCell();
}
}

+ 21
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/word/package-info.java View File

@@ -0,0 +1,21 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Word 模块,因为2003不好用,所以都是基于2007来处理的数据
* @author JueYue
* @date 2014年6月20日 上午12:08:09
*/
package org.jeecgframework.poi.word;

Loading…
Cancel
Save