보증서발행완료 zip파일 다운로드시 데이터가 없는 경우 no_contents 처리
This commit is contained in:
parent
90930a11f1
commit
d886e2a27a
@ -64,9 +64,9 @@ public class ExcelDownController {
|
||||
|
||||
@Operation(description = "과거데이터_보증서발행완료 물건 파일 다운로드 조회")
|
||||
@PostMapping("/wrnt-isnc-cmpl-file-down")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void warrantyIssuedCmpFileDown(@RequestBody WrntIsncCmplRequest wrntIsncCmplRequest,
|
||||
HttpServletResponse response) throws Exception {
|
||||
public void warrantyIssuedCmpFileDown(
|
||||
@RequestBody WrntIsncCmplRequest wrntIsncCmplRequest, HttpServletResponse response)
|
||||
throws Exception {
|
||||
try {
|
||||
excelDownService.selectWarrantyIssuedCmpFileData(wrntIsncCmplRequest, response);
|
||||
} catch (Exception e) {
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
package com.interplug.qcast.biz.excelDown;
|
||||
|
||||
import com.interplug.qcast.biz.excelDown.dto.*;
|
||||
import com.interplug.qcast.config.Exception.ErrorCode;
|
||||
import com.interplug.qcast.config.Exception.QcastException;
|
||||
import com.interplug.qcast.config.message.Messages;
|
||||
import com.interplug.qcast.util.ZipFileManager;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@ -66,9 +64,12 @@ public class ExcelDownService {
|
||||
}
|
||||
|
||||
zipFileManager.createZipFile(response, "Warranty-issued-completed-files", listFile);
|
||||
|
||||
} else {
|
||||
throw new QcastException(
|
||||
ErrorCode.NOT_FOUND, message.getMessage("common.message.no.dataDown"));
|
||||
// 데이터가 없을 경우 NO_CONTENT 상태 코드 설정 후 종료
|
||||
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
|
||||
response.getWriter().write(message.getMessage("common.message.no.dataDown"));
|
||||
response.flushBuffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,37 +1,37 @@
|
||||
package com.interplug.qcast.util;
|
||||
|
||||
import com.interplug.qcast.config.message.Messages;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.interplug.qcast.config.message.Messages;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ZipFileManager {
|
||||
|
||||
@Autowired
|
||||
Messages message;
|
||||
@Autowired Messages message;
|
||||
|
||||
/**
|
||||
* zip file을 생성하여 ZipOutputStream를 반환하는 method
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @param strCreateFileName 생성할 file 이름
|
||||
* @param listFilePath 파일 정보 목록
|
||||
* @param listFile 파일 정보 목록
|
||||
*/
|
||||
public void createZipFile(HttpServletResponse response, String strCreateFileName,
|
||||
List<Map<String, String>> listFile) throws Exception {
|
||||
public void createZipFile(
|
||||
HttpServletResponse response, String strCreateFileName, List<Map<String, String>> listFile)
|
||||
throws Exception {
|
||||
|
||||
// 압축될 파일명이 존재하지 않을 경우
|
||||
if (strCreateFileName == null || "".equals(strCreateFileName))
|
||||
throw new IllegalArgumentException(
|
||||
@ -44,7 +44,27 @@ public class ZipFileManager {
|
||||
// zip 파일명
|
||||
String strZipName = strCreateFileName + ".zip";
|
||||
|
||||
// response 설정
|
||||
// 파일이 실제로 존재하는지 확인하기 위해 카운트
|
||||
int cnt = 0;
|
||||
for (Map<String, String> mapFile : listFile) {
|
||||
String strSourceFile = mapFile.get("filename");
|
||||
Path path = Path.of(strSourceFile);
|
||||
if (path.toFile().exists()) {
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("cnt :: " + cnt);
|
||||
|
||||
// 파일이 없을 경우 상태 코드와 메시지 설정 후 종료
|
||||
if (cnt == 0) {
|
||||
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
|
||||
response.getWriter().write("No files available for download.");
|
||||
response.flushBuffer();
|
||||
return; // 메서드 종료
|
||||
}
|
||||
|
||||
// 파일이 있을 경우에만 ZIP 파일 생성
|
||||
response.setContentType("application/zip");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + strZipName + ";");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
@ -55,43 +75,32 @@ public class ZipFileManager {
|
||||
String strSourceFile = mapFile.get("filename");
|
||||
Path path = Path.of(strSourceFile);
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(path.toFile())) {
|
||||
// 압축될 파일명을 ZipEntry에 담아준다
|
||||
ZipEntry zipEntry = new ZipEntry(strDirectory + "\\" + path.getFileName().toString());
|
||||
if (!path.toFile().exists()) {
|
||||
log.debug("파일 변환 작업중, [ " + strSourceFile + " ] 파일을 찾을 수 없습니다.");
|
||||
continue; // 파일이 없으면 스킵
|
||||
}
|
||||
|
||||
// 압축될 파일명을 ZipOutputStream 에 담아준다
|
||||
try (FileInputStream fis = new FileInputStream(path.toFile())) {
|
||||
ZipEntry zipEntry = new ZipEntry(strDirectory + "\\" + path.getFileName().toString());
|
||||
zos.putNextEntry(zipEntry);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
|
||||
while ((length = fis.read(buffer)) >= 0) {
|
||||
zos.write(buffer, 0, length);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
// response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
// throw new IllegalArgumentException("파일 변환 작업중, [ " + strSourceFile + " ] 파일을 찾을 수
|
||||
// 없습니다.");
|
||||
log.debug("파일 변환 작업중, [ " + strSourceFile + " ] 파일을 찾을 수 없습니다.");
|
||||
} catch (IOException e) {
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
throw new IllegalArgumentException(
|
||||
"파일 변환 작업중, [ " + strSourceFile + " ] 파일을 다운로드 할 수 없습니다.");
|
||||
} finally {
|
||||
// ZipOutputStream 에 담아둔 압축될 파일명을 flush 시켜준다
|
||||
zos.flush();
|
||||
// ZipOutputStream 에 담아둔 압축될 파일명을 close 시켜준다
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
// response 에 담아둔 파일을 flush 시켜준다
|
||||
response.flushBuffer();
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
response.flushBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user