Browse Source

* 在临时文件被删除的情况下能正常的读取 [Issue #2693](https://github.com/alibaba/easyexcel/issues/2693)

pull/2959/head
Jiaju Zhuang 2 years ago
parent
commit
0afb6aa81e
  1. 9
      easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java
  2. 41
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java
  3. 13
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
  4. 1
      update.md

9
easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java vendored

@ -2,6 +2,7 @@ package com.alibaba.excel.cache;
import java.io.File;
import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID;
import com.alibaba.excel.context.AnalysisContext;
@ -79,7 +80,8 @@ public class Ehcache implements ReadCache {
static {
CACHE_PATH_FILE = FileUtils.createCacheTmpFile();
FILE_CACHE_MANAGER =
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(CACHE_PATH_FILE)).build(true);
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(CACHE_PATH_FILE)).build(
true);
ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true);
FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class, ResourcePoolsBuilder.newResourcePoolsBuilder()
@ -92,13 +94,16 @@ public class Ehcache implements ReadCache {
try {
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
} catch (IllegalStateException e) {
//fix Issue #2693,重建缓存文件夹
//fix Issue #2693,Temporary files may be deleted if there is no operation for a long time, so they need
// to be recreated.
if (CACHE_PATH_FILE.exists()) {
throw e;
}
synchronized (Ehcache.class) {
if (!CACHE_PATH_FILE.exists()) {
if (log.isDebugEnabled()) {
log.debug("cache file dir is not exist retry create");
}
FileUtils.createDirectory(CACHE_PATH_FILE);
}
}

41
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java

@ -1,16 +1,24 @@
package com.alibaba.easyexcel.test.core.compatibility;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.easyexcel.test.core.large.LargeData;
import com.alibaba.easyexcel.test.core.large.LargeDataListener;
import com.alibaba.easyexcel.test.core.simple.SimpleData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.cache.Ehcache;
import com.alibaba.excel.constant.EasyExcelConstants;
import com.alibaba.excel.enums.ReadDefaultReturnEnum;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.TempFile;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
@ -116,4 +124,37 @@ public class CompatibilityTest {
Assert.assertEquals("24.20", list.get(0).get(11));
}
@Test
public void t08() {
// https://github.com/alibaba/easyexcel/issues/2693
// Temporary files may be deleted if there is no operation for a long time, so they need to be recreated.
File file = TestFileUtil.createNewFile("compatibility/t08.xlsx");
EasyExcel.write(file, SimpleData.class)
.sheet()
.doWrite(data());
List<Map<Integer, Object>> list = EasyExcel.read(file)
.readCache(new Ehcache(null, 20))
.sheet()
.doReadSync();
Assert.assertEquals(10L, list.size());
FileUtils.delete(new File(System.getProperty(TempFile.JAVA_IO_TMPDIR)));
list = EasyExcel.read(file)
.readCache(new Ehcache(null, 20))
.sheet()
.doReadSync();
Assert.assertEquals(10L, list.size());
}
private List<SimpleData> data() {
List<SimpleData> list = new ArrayList<SimpleData>();
for (int i = 0; i < 10; i++) {
SimpleData simpleData = new SimpleData();
simpleData.setName("姓名" + i);
list.add(simpleData);
}
return list;
}
}

13
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java

@ -133,18 +133,7 @@ public class LargeDataTest {
LOGGER.info("{} vs {}", cost, costPoi);
Assert.assertTrue(costPoi * 2 > cost);
}
@Test
public void t05Read() throws Exception {
long start = System.currentTimeMillis();
EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class,
new LargeDataListener()).headRowNumber(2).sheet().doRead();
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
FileUtils.delete(new File(System.getProperty(TempFile.JAVA_IO_TMPDIR)));
start = System.currentTimeMillis();
EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class,
new LargeDataListener()).headRowNumber(2).sheet().doRead();
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
}
private List<LargeData> data() {
List<LargeData> list = new ArrayList<>();
int size = i + 100;

1
update.md

@ -2,6 +2,7 @@
* 兼容`LocalDate` [Issue #2908](https://github.com/alibaba/easyexcel/issues/2908)
* 优化大文件内存存储,减少内存占用 [Issue #2657](https://github.com/alibaba/easyexcel/issues/2657)
* 在临时文件被删除的情况下能正常的读取 [Issue #2693](https://github.com/alibaba/easyexcel/issues/2693)
# 3.2.0

Loading…
Cancel
Save