Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -546,19 +546,14 @@ public void truncate() throws IOException {
*/
private void initializeBufferedWriter() throws IOException {
File file = resource.getFile();

FileUtils.setUpOutputFile(file, restarted, append, shouldDeleteIfExists);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(file.getAbsolutePath(), true);
try (FileOutputStream stream = new FileOutputStream(file.getAbsolutePath(), true)) {
fileChannel = stream.getChannel();
outputBufferedWriter = getBufferedWriter(fileChannel, encoding);
outputBufferedWriter.flush();

if (append) {
if (file.length() > 0) {
appending = true;
}
if (append && file.length() > 0) {
appending = true;
}

Assert.state(outputBufferedWriter != null, "");
Expand All @@ -571,16 +566,6 @@ private void initializeBufferedWriter() throws IOException {
initialized = true;
linesWritten = 0;
os = stream;
stream = null;
// 2026.02.28 KISA 보안취약점 조치
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
LOGGER.debug("EgovPartitionFlatFileItemWriter initializeBufferedWriter() : {}", e.getClass().getName(), e.getMessage());
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ public byte[] encrypt(byte[] data, String password) {
}

public void encrypt(File srcFile, String password, File trgtFile) {
FileInputStream fis = null;
ByteArrayOutputStream baos = null;
String fileString;
byte[] buffer;
if (passwordEncoder.checkPassword(password)) {
ARIACipher cipher = new ARIACipher();
cipher.setPassword(password);
try {
fis = new FileInputStream(srcFile);
baos = new ByteArrayOutputStream();
try (
FileInputStream fis = new FileInputStream(srcFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream()
) {
buffer = new byte[blockSize];
LOGGER.debug("blockSize = {}", blockSize);
int len = 0;
Expand All @@ -95,8 +94,6 @@ public void encrypt(File srcFile, String password, File trgtFile) {
FileUtils.writeStringToFile(trgtFile, encString, "UTF-8", true);
} catch (IOException e) {
ReflectionUtils.handleReflectionException(e);
} finally {
EgovResourceReleaser.close(fis, baos);
}
} else {
throw new IllegalArgumentException("password not matched!!!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,19 @@ public BigDecimal encrypt(BigDecimal number, String password) {
}

public void encrypt(File srcFile, String password, File trgtFile) {
FileInputStream fis = null;
FileWriter fw = null;
BufferedInputStream bis = null;
BufferedWriter bw = null;
byte[] buffer = null;
if (passwordEncoder.checkPassword(password)) {
StandardPBEByteEncryptor cipher = new StandardPBEByteEncryptor();
cipher.setAlgorithm(algorithm);
cipher.setPassword(password);
buffer = new byte[blockSize];
LOGGER.debug("blockSize = {}", blockSize);
try {
fis = new FileInputStream(srcFile);
bis = new BufferedInputStream(fis);
fw = new FileWriter(trgtFile);
bw = new BufferedWriter(fw);
try (
FileInputStream fis = new FileInputStream(srcFile);
FileWriter fw = new FileWriter(trgtFile);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedWriter bw = new BufferedWriter(fw)
) {
byte[] encrypted = null;
int length = 0;
long size = 0L;
Expand All @@ -115,8 +112,6 @@ public void encrypt(File srcFile, String password, File trgtFile) {
LOGGER.debug("processed bytes = {}", size);
} catch (IOException e) {
ReflectionUtils.handleReflectionException(e);
} finally {
EgovResourceReleaser.close(fw, bw, fis, bis);
}
} else {
throw new IllegalArgumentException("password not matched!!!");
Expand Down Expand Up @@ -146,19 +141,16 @@ public BigDecimal decrypt(BigDecimal encryptedNumber, String password) {
}

public void decrypt(File encryptedFile, String password, File trgtFile) {
FileReader fr = null;
FileOutputStream fos = null;
BufferedReader br = null;
BufferedOutputStream bos = null;
if (passwordEncoder.checkPassword(password)) {
StandardPBEByteEncryptor cipher = new StandardPBEByteEncryptor();
cipher.setAlgorithm(algorithm);
cipher.setPassword(password);
try {
fr = new FileReader(encryptedFile);
br = new BufferedReader(fr);
fos = new FileOutputStream(trgtFile);
bos = new BufferedOutputStream(fos);
try (
FileReader fr = new FileReader(encryptedFile);
FileOutputStream fos = new FileOutputStream(trgtFile);
BufferedReader br = new BufferedReader(fr);
BufferedOutputStream bos = new BufferedOutputStream(fos)
) {
byte[] encrypted = null;
byte[] decrypted = null;
String line = null;
Expand All @@ -170,8 +162,6 @@ public void decrypt(File encryptedFile, String password, File trgtFile) {
bos.flush();
} catch (IOException e) {
ReflectionUtils.handleReflectionException(e);
} finally {
EgovResourceReleaser.close(fos, bos, fr, br);
}
} else {
throw new IllegalArgumentException("password not matched!!!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ private byte[] getHashValue(File file) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DigestOutputStream dout = new DigestOutputStream(baos, sha);
byte[] data = new byte[1024];
FileInputStream fis = null;
BufferedInputStream is = null;

try {
fis = new FileInputStream(file);
is = new BufferedInputStream(fis);
try (
FileInputStream fis = new FileInputStream(file);
BufferedInputStream is = new BufferedInputStream(fis)
) {
while (true) {
int bytesRead = is.read(data);
if (bytesRead < 0)
Expand All @@ -51,21 +50,6 @@ private byte[] getHashValue(File file) throws Exception {
dout.flush();
byte[] result = dout.getMessageDigest().digest();
return result;
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
fail(e.getMessage());
}
}
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
fail(e.getMessage());
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,20 @@ public void setMapBeanName(String mapBeanName) {
*/
@Override
public Workbook createWorkbook(Workbook wb, String filepath) {
String fullPath = FilenameUtils.getFullPath(filepath);
try {
LOGGER.debug("EgovExcelServiceImpl.createWorkbook 1 : templatePath is {}", FilenameUtils.getFullPath(filepath));
if (!EgovFileUtil.isExistsFile(FilenameUtils.getFullPath(filepath))) {
LOGGER.debug("make dir {}", FilenameUtils.getFullPath(filepath));
FileUtils.forceMkdir(new File(FilenameUtils.getFullPath(filepath)));
LOGGER.debug("EgovExcelServiceImpl.createWorkbook 1 : templatePath is {}", fullPath);
if (!EgovFileUtil.isExistsFile(fullPath)) {
LOGGER.debug("make dir {}", fullPath);
FileUtils.forceMkdir(new File(fullPath));
}
FileOutputStream fileOut = null;

LOGGER.debug("EgovExcelServiceImpl.createWorkbook 2 : templatePath is {}", filepath);
try {
try (FileOutputStream fileOut = new FileOutputStream(filepath);) {
LOGGER.debug("ExcelServiceImpl filepath ...");
fileOut = new FileOutputStream(filepath);
wb.write(fileOut);
} finally {
LOGGER.debug("ExcelServiceImpl loadExcelObject End ");
if (wb != null) wb.close();
if (fileOut != null) fileOut.close();
}

return wb;
} catch (IOException e) {
throw new BaseRuntimeException("IOException: createWorkbook", e);
Expand All @@ -145,19 +142,13 @@ public Workbook createWorkbook(Workbook wb, String filepath) {
*/
@Override
public Workbook loadExcelTemplate(String templateName) {
try {
FileInputStream fileIn = null;
Workbook wb = null;
LOGGER.debug("EgovExcelServiceImpl.loadExcelTemplate : templatePath is {}", templateName);
try {
LOGGER.debug("ExcelServiceImpl loadExcelTemplate ...");
fileIn = new FileInputStream(templateName);
wb = new HSSFWorkbook(fileIn);
} finally {
LOGGER.debug("ExcelServiceImpl loadExcelTemplate End ");
if (wb != null) wb.close();
if (fileIn != null) fileIn.close();
}
LOGGER.debug("EgovExcelServiceImpl.loadExcelTemplate : templatePath is {}", templateName);

try (
FileInputStream fileIn = new FileInputStream(templateName);
Workbook wb = new HSSFWorkbook(fileIn)
) {
LOGGER.debug("ExcelServiceImpl loadExcelTemplate ...");
return wb;
} catch (IOException e) {
throw new BaseRuntimeException("IOException: loadExcelTemplate(String templateName)", e);
Expand All @@ -169,39 +160,32 @@ public Workbook loadExcelTemplate(String templateName) {
*/
@Override
public XSSFWorkbook loadExcelTemplate(String templateName, XSSFWorkbook wb) {
LOGGER.debug("EgovExcelServiceImpl.loadExcelTemplate(XSSF) : templatePath is {}", templateName);

try {
FileInputStream fileIn = null;
LOGGER.debug("EgovExcelServiceImpl.loadExcelTemplate(XSSF) : templatePath is {}", templateName);
try {
try (FileInputStream fileIn = new FileInputStream(templateName)) {
LOGGER.debug("ExcelServiceImpl loadExcelTemplate(XSSF) ...");
fileIn = new FileInputStream(templateName);
wb = new XSSFWorkbook(fileIn);
} finally {
LOGGER.debug("ExcelServiceImpl loadExcelTemplate(XSSF) End ");
if (wb != null) wb.close();
if (fileIn != null) fileIn.close();
}
return wb;
} catch (IOException e) {
throw new BaseRuntimeException("IOException: loadExcelTemplate(String templateName, XSSFWorkbook wb)", e);
}

return wb;
}

/**
* 엑셀 파일을 로딩한다.
*/
@Override
public Workbook loadWorkbook(String filepath) {
try {
FileInputStream fileIn = null;
Workbook wb = null;
try {
fileIn = new FileInputStream(filepath);
wb = loadWorkbook(fileIn);
} finally {
if (wb != null) wb.close();
if (fileIn != null) fileIn.close();
}
try (
FileInputStream fileIn = new FileInputStream(filepath);
Workbook wb = loadWorkbook(fileIn)
) {
return wb;
} catch (IOException e) {
throw new BaseRuntimeException("IOException: loadWorkbook(String filepath)", e);
Expand All @@ -213,18 +197,19 @@ public Workbook loadWorkbook(String filepath) {
*/
@Override
public XSSFWorkbook loadWorkbook(String filepath, XSSFWorkbook wb) {
try {
FileInputStream fileIn = null;
try {
fileIn = new FileInputStream(filepath);
wb = loadWorkbook(fileIn, wb);
} finally {
if (wb != null) wb.close();
if (fileIn != null) fileIn.close();
}
try (FileInputStream fileIn = new FileInputStream(filepath)) {
wb = loadWorkbook(fileIn, wb);
return wb;
} catch (IOException e) {
throw new BaseRuntimeException("IOException: loadWorkbook(String filepath, XSSFWorkbook wb)", e);
} finally {
if (wb != null) {
try {
wb.close();
} catch (IOException e) {
LOGGER.error("IOException: loadWorkbook(String filepath, XSSFWorkbook wb)", e);
}
}
}
}

Expand All @@ -234,18 +219,15 @@ public XSSFWorkbook loadWorkbook(String filepath, XSSFWorkbook wb) {
@Override
public Workbook loadWorkbook(InputStream fileIn) {
try {
POIFSFileSystem fs = null;
Workbook wb = null;
try {
try (
POIFSFileSystem fs = new POIFSFileSystem(fileIn);
Workbook wb = new HSSFWorkbook(fs)
) {
LOGGER.debug("ExcelServiceImpl loadWorkbook ...");
fs = new POIFSFileSystem(fileIn);
wb = new HSSFWorkbook(fs);
return wb;
} finally {
if (wb != null) wb.close();
if (fs != null) fs.close();
if (fileIn != null) fileIn.close();
}
return wb;
} catch (IOException e) {
throw new BaseRuntimeException("Workbook loadWorkbook(InputStream fileIn)", e);
}
Expand All @@ -256,23 +238,18 @@ public Workbook loadWorkbook(InputStream fileIn) {
* POI XSSFWorkbook(InputStream)은 mark/reset 지원이 필요하므로, 미지원 시 BufferedInputStream으로 감싼다.
*/
public XSSFWorkbook loadWorkbook(InputStream fileIn, XSSFWorkbook wb) {
InputStream streamToUse = null;
if (fileIn == null) {
throw new BaseRuntimeException("FileInputStream is null");
}

// 2026.02.28 KISA 보안취약점 조치
try {
try (InputStream streamToUse = fileIn.markSupported() ? fileIn : new BufferedInputStream(fileIn)) {
LOGGER.debug("ExcelServiceImpl loadWorkbook(XSSF) ...");
streamToUse = (fileIn != null && fileIn.markSupported()) ? fileIn : new BufferedInputStream(fileIn);
wb = new XSSFWorkbook(streamToUse);

return wb;
} catch (IOException e) {
throw new BaseRuntimeException("XSSFWorkbook loadWorkbook(InputStream fileIn, XSSFWorkbook wb)", e);
} finally {
if (streamToUse != null && streamToUse != fileIn) {
try {
streamToUse.close();
} catch (IOException e) {
LOGGER.debug("Failed to close stream in loadWorkbook(XSSF): {}", e.getMessage());
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ public void onSetUp() throws SQLException {
@Test
public void testUploadExcelFile() {
LOGGER.debug("### EgovExcelUploadTest testUploadExcelFile() Start ");
try {
FileInputStream fileIn = new FileInputStream(new File("testdata/testBatch.xls"));
try (FileInputStream fileIn = new FileInputStream(new File("testdata/testBatch.xls"))) {
excelService.uploadExcel("insertEmpUsingBatch", fileIn);
} catch (Exception e) {
LOGGER.debug("[{}] EgovExcelUploadTest testUploadExcelFile() : {}", e.getMessage());
LOGGER.debug("[{}] EgovExcelUploadTest testUploadExcelFile() : {}", e.getClass().getName(), e.getMessage());
} finally {
LOGGER.debug("### EgovExcelUploadTest testUploadExcelFile() End ");
}
Expand All @@ -67,8 +66,7 @@ public void testUploadExcelFile() {
@Test
public void testBigUploadExcelFile() {
LOGGER.debug("### EgovExcelUploadTest testBigUploadExcelFile() Start ");
try {
FileInputStream fileIn = new FileInputStream(new File("testdata/zipExcel.xls"));
try (FileInputStream fileIn = new FileInputStream(new File("testdata/zipExcel.xls"))) {
excelBigService.uploadExcel("insertZipUsingBatch", fileIn, 2, (long) 5000);
} catch (Exception e) {
LOGGER.debug("### EgovExcelUploadTest testBigUploadExcelFile() Exception : {}", e.getMessage());
Expand Down
Loading