반응형
# 파일 다운로드 (zip)
## context-util.xml
<util:properties id="fileUploadProp">
<prop key="hrPhotoUploadPath">/APP/mis/MIS_DATA/upload/hr/photo</prop>
<prop key="hrPhotoUploadPath">/APP/mis/MIS_DATA/upload/photoZip</prop>
</util:properties>
## JAVA
@Value("#{fileUploadProp['hrPhotoUploadPath']}")
private String photoAccessUrl;
@Value("#{fileUploadProp['hrPhotoAccessPath']}")
private String photoAccessPath;
@RequestMapping(value="goAllDownload.ex")
public void allDownload(HttpServletRequest request, HttpServletResponse response, @RequestParam("imgEmpNo") String tmpEmpNo) throws Exception {
String[] empNos = tmpEmpNo.split(",");
for (int i = 0; i < empNos.length; i++) {
if (empNos[i] == null || "".equals(empNos[i].trim())) {
throw new BusinessException("errors.required", new String[]{"@title.bmp"});
}
}
// 파일 저장 경로.
String path = photoAccessPath;
String zipFileName = "파일명_" + System.currentTimeMillis() + ".zip";
String zipFile = path + zipFileName;
File file = null;
ArrayList<String> fileList = new ArrayList<String>();
for (int i = 0; i < empNos.length; i++) {
String fileUrl = photoAccessUrl + "P" + empNos[i] + ".BMP";
file = new File(fileUrl);
// 파일 존재여부 체크
if (file.exists()) {
if (file.isFile()) {
fileList.add(fileUrl);
}
}
}
if (fileList.size() != 0) {
FileOutputStream fos = null;
FileInputStream fis = null;
ZipOutputStream zipOut = null;
try {
fos = new FileOutputStream(zipFile);
zipOut = new ZipOutputStream(fos);
for (int i = 0; i < fileList.size(); i++) {
File fileSize = new File(fileList.get(i));
byte[] buf = new byte[(int)fileSize.length()];
fis = new FileInputStream(fileList.get(i));
path p = paths.get(fileList.get(i));
String fileName = p.getFileName().toString();
ZipEntry ze = new ZipEntry(fileName);
zipOut.putNextEntry(ze);
int len = 0;
while((len = in.read(buf)) != -1) {
zipOut.write(buf, 0, len);
}
fis.close();
zipOut.closeEntry();
}
zipOut.close();
fos.close();
response.setContentType("application/zip");
response.setHeader("Content-Transfer-Encoding:", "binary");
response.setHeader("Content-Disposition", "attachment; filename=" + zipFileName);
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
// 다운로드 위해 추가.
FileInputStream fiss = new FileInputStream(zipFile);
BufferedInputStream bis = new BufferedInputStream(fiss);
ServletOutputStream so = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(so);
File fileSizeZip = new File(zipFile);
byte[] data = new byte[(int)fileSizeZip.length()];
int input = 0;
while((input = bis.read(data)) != -1) {
bos.write(data, 0, input);
bos.flush();
}
if (bos != null) bos.close();
if (bis != null) bis.close();
if (so != null) so.close();
if (fiss != null) fiss.close();
} catch(IOException e) {
e.printStackTrace();
} finally {
// 저장경로에 임시저장 된 zip 파일 삭제.
File createZipFile = new File(zipFile);
if (createZipFile.exists()) createZipFile.delete();
}
}
}
## 문제점
- 정해진 위치에 zip 파일 다운로드 정상적으로 수행(정해진 경로에서 파일 열었을땐 이상 없음). 하지만, 파일 다운로드 수행 시 열기, 저장 클릭 후 해당 파일 열 경우 zip 파일이 아니라는 에러 발생.
- 파일사이즈 관련 문제 발생.
## 해결방법
- 다운로드 위한 코드 추가하여 해결.
- 추가로 별도의 경로에 임시 저장된 zip파일 삭제 코드 추가.
- 고정된 파일 사이즈에서 해당 파일의 사이즈로 변경
반응형
'메모장' 카테고리의 다른 글
오라클 일련번호 증가 쿼리 (0) | 2022.05.10 |
---|---|
WEB, WAS, WEB과 WAS의 차이점 (0) | 2022.05.04 |
엑셀 다운로드 (자바, 자바스크립트) (0) | 2022.04.24 |
context-util properties 관련 (0) | 2022.04.24 |
onkeyup, onkeydown 관련 중복 조회 이슈 및 해결방법 (0) | 2022.04.24 |