From f22212b70bd45cbaaf0dc536fa011607638afdd2 Mon Sep 17 00:00:00 2001 From: Marten Deinum Date: Thu, 3 Feb 2022 08:29:48 +0100 Subject: [PATCH] Improve resource file check (#90) With this commit we use the isFile check introduced in Spring 5 to check if something is an actaul file resource instead of relying on FileNotFoundException. Resource implementations didn't consistently throw a FileNotFoundException (like the Google Big Query one) leading to issues with reading. Closes: 89 --- .../batch/extensions/excel/poi/PoiItemReader.java | 7 ++----- .../excel/streaming/StreamingXlsxItemReader.java | 5 ++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/poi/PoiItemReader.java b/spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/poi/PoiItemReader.java index 0582b79..541c710 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/poi/PoiItemReader.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/poi/PoiItemReader.java @@ -17,7 +17,6 @@ package org.springframework.batch.extensions.excel.poi; import java.io.File; -import java.io.FileNotFoundException; import java.io.InputStream; import org.apache.poi.ss.usermodel.Row; @@ -80,16 +79,14 @@ public class PoiItemReader extends AbstractExcelItemReader { */ @Override protected void openExcelFile(final Resource resource, String password) throws Exception { - - try { + if (resource.isFile()) { File file = resource.getFile(); this.workbook = WorkbookFactory.create(file, password, false); } - catch (FileNotFoundException ex) { + else { this.inputStream = resource.getInputStream(); this.workbook = WorkbookFactory.create(this.inputStream, password); } this.workbook.setMissingCellPolicy(Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); } - } diff --git a/spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/streaming/StreamingXlsxItemReader.java b/spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/streaming/StreamingXlsxItemReader.java index 444984d..6441c7a 100644 --- a/spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/streaming/StreamingXlsxItemReader.java +++ b/spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/streaming/StreamingXlsxItemReader.java @@ -17,7 +17,6 @@ package org.springframework.batch.extensions.excel.streaming; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -63,11 +62,11 @@ public class StreamingXlsxItemReader extends AbstractExcelItemReader { @Override protected void openExcelFile(Resource resource, String password) throws Exception { - try { + if (resource.isFile()) { File file = resource.getFile(); this.pkg = OPCPackage.open(file, PackageAccess.READ); } - catch (FileNotFoundException ex) { + else { this.inputStream = resource.getInputStream(); this.pkg = OPCPackage.open(this.inputStream); }