다운로드 기능 보완

This commit is contained in:
KNKIM 2021-08-03 11:39:03 +09:00
parent 8bf7d93ec0
commit 3a47fb8185
8 changed files with 102 additions and 32 deletions

View File

@ -1,4 +1,4 @@
/F20210716045132766_5A30C3D2F8AB44A6867D76329E4502E2 /*
/F20210716045132766_B48A0FCFBE2D46D290B31DDECB61DA9B !/F20210802052436092_0A84FA72B1B1478ABEB285536B81B587
/F20210716045247870_C228F5BAD6894E66A50B48F63CA8916C !/F20210802052436092_F1DB407F181A4B8AAA0859E56D4EDB57
/F20210716045247870_E8D7EB4610604852982585486F9BAD5A

View File

@ -23,27 +23,28 @@ public class DownloadView extends AbstractView {
File file = (File)model.get("downloadFile"); File file = (File)model.get("downloadFile");
if(file != null) { if(file != null) {
String fileName =(String)model.get("fname"); String fileName =(String)model.get("fname");
String encfileName = null;
String userAgent = request.getHeader("User-Agent"); String userAgent = request.getHeader("User-Agent");
if(userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1){ if(userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1){
//fileName = URLEncoder.encode(file.getName(), "utf-8").replaceAll("\\+", "%20");; encfileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");;
}else if(userAgent.indexOf("Chrome") > -1) { }else if(userAgent.indexOf("Chrome") > -1) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for(int i=0; i<file.getName().length(); i++) { for(int i=0; i<fileName.length(); i++) {
char c = file.getName().charAt(i); char c = fileName.charAt(i);
if(c > '~') { if(c > '~') {
sb.append(URLEncoder.encode(""+c, "UTF-8")); sb.append(URLEncoder.encode(""+c, "UTF-8"));
}else { }else {
sb.append(c); sb.append(c);
} }
} }
//fileName = sb.toString(); encfileName = sb.toString();
}else { }else {
//fileName = new String(file.getName().getBytes("utf-8")); encfileName = new String(fileName.getBytes("utf-8"));
} }
response.setContentType(getContentType()); response.setContentType(getContentType());
response.setContentLength((int)file.length()); response.setContentLength((int)file.length());
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";"); response.setHeader("Content-Disposition", "attachment; filename=\"" + encfileName + "\";");
response.setHeader("Content-Transfer-Encoding", "binary"); response.setHeader("Content-Transfer-Encoding", "binary");
OutputStream out = response.getOutputStream(); OutputStream out = response.getOutputStream();

View File

@ -77,6 +77,21 @@ public class FileUploadController {
*/ */
private String FILEUPLOAD_TEMP_SUBPATH = nlibProperty.getProperty("fileupload.temp.subpath"); private String FILEUPLOAD_TEMP_SUBPATH = nlibProperty.getProperty("fileupload.temp.subpath");
/**
* 경로 부적합 오류 메시지
*/
private static final String MSG_NOT_VALID_FILE_PATH = "파일 경로가 부적합합니다. 관리자에게 문의하여 주시기 바랍니다.";
/**
* 파일업로드(Ajax) 처리
* - 화면 : Javascript Free Open Framework DropZone 사용
*
* @param multiRequest
* @param subPathKey
* @param response : 파일정보를 담은 JSON String
* @return
*/
@ResponseBody @ResponseBody
@RequestMapping(value="/fileupload/uploadFilesAjax.do", produces="application/json") @RequestMapping(value="/fileupload/uploadFilesAjax.do", produces="application/json")
public ResponseEntity uploadFilesAjax( public ResponseEntity uploadFilesAjax(
@ -84,22 +99,47 @@ public class FileUploadController {
, @RequestParam("subPathKey") String subPathKey , @RequestParam("subPathKey") String subPathKey
, HttpServletResponse response) { , HttpServletResponse response) {
// TODO : 권한 체크 추가할 String message = null;
// TODO : 권한 체크 추가할
List<FileVO> result = new ArrayList<FileVO>(); List<FileVO> result = new ArrayList<FileVO>();
try { try {
// 최상위 위치 // 최상위 위치
if(FileUtil.isNotValid(FILEUPLOAD_BASE_PATH)) throw new Exception("첨부파일이 저장되는 최상위 위치값이 적절하지 않습니다."); if(FileUtil.isEmpty(FILEUPLOAD_BASE_PATH)) {
message = "첨부파일이 저장되는 최상위 위치정보값이 정확하지 않습니다.";
log.error(message + " : FILEUPLOAD_BASE_PATH 값 부재");
ErrorMessage errorMessage = new ErrorMessage("ERROR", message);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}
// 서브 위치 // 서브 위치
if(StringUtil.isEmpty(subPathKey)) throw new Exception("첨부파일이 저장되는 위치정보값이 정확하지 않습니다."); if(StringUtil.isEmpty(subPathKey)) {
message = "첨부파일이 저장되는 하위 위치정보값이 정확하지 않습니다.";
log.error(message + " : 매개변수 subPathKey 값 부재");
ErrorMessage errorMessage = new ErrorMessage("ERROR", message);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}
String subPath = nlibProperty.getProperty(subPathKey); String subPath = nlibProperty.getProperty(subPathKey);
if(FileUtil.isNotValid(subPath)) throw new Exception("첨부파일 서브 위치값이 적절하지 않습니다."); if(StringUtil.isEmpty(subPath)) {
message = "첨부파일이 저장되는 하위 위치정보값을 찾을 수 없습니다.";
log.error(message + " : 매개변수 subPathKey의 속성값 부재");
ErrorMessage errorMessage = new ErrorMessage("ERROR", message);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}
// 위치정보 적합 확인
if(FileUtil.isNotValid(FILEUPLOAD_BASE_PATH + subPath)) {
message = MSG_NOT_VALID_FILE_PATH;
log.error(message + " : " + (FILEUPLOAD_BASE_PATH + subPath));
ErrorMessage errorMessage = new ErrorMessage("ERROR", message);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}
// 파일 업로드 처리
final Map<String, MultipartFile> files = multiRequest.getFileMap(); final Map<String, MultipartFile> files = multiRequest.getFileMap();
log.debug("uploadFilesAjax : 진입 " + (files == null ? " files null " : files.size())); log.debug("uploadFilesAjax : 진입 > 파일수 = " + (files == null ? " files null " : files.size()));
File dir = new File(FILEUPLOAD_BASE_PATH + subPath); File dir = new File(FILEUPLOAD_BASE_PATH + subPath);
if(!dir.exists() || !dir.isDirectory()) dir.mkdirs(); if(!dir.exists() || !dir.isDirectory()) dir.mkdirs();
@ -119,37 +159,30 @@ public class FileUploadController {
String orginFileName = file.getOriginalFilename(); String orginFileName = file.getOriginalFilename();
//-------------------------------------- //--------------------------------------
// 파일명이 없는 경우 처리 // 파일명이 없는 경우 처리 SKIP
// (첨부가 되지 않은 input file type) // (첨부가 되지 않은 input file type)
//-------------------------------------- //--------------------------------------
if ("".equals(orginFileName)) { if (orginFileName == null || "".equals(orginFileName)) {
continue; continue;
} }
////------------------------------------
int index = orginFileName.lastIndexOf("."); int index = orginFileName.lastIndexOf(".");
//String fileName = orginFileName.substring(0, index); String fileExt = (index < 1 ? "" : orginFileName.substring(index + 1));
String fileExt = orginFileName.substring(index + 1);
//String newName = KeyStr + getTimeStamp() + fileKey;
String newName = UUID.getPhysicalFileName(); String newName = UUID.getPhysicalFileName();
long size = file.getSize(); long size = file.getSize();
log.debug("FILE UPLOAD : File New Name=" + newName);
if (!"".equals(orginFileName)) { if (!"".equals(orginFileName)) {
filePath = FILEUPLOAD_BASE_PATH + subPath + "/" + newName; filePath = FILEUPLOAD_BASE_PATH + subPath + "/" + newName;
log.debug("FILE UPLOAD : filePath=" + filePath); log.debug("FILE UPLOAD : filePath=" + filePath);
file.transferTo(new File(EgovWebUtil.filePathBlackList(filePath))); file.transferTo(new File(FileUtil.filePathBlackList(filePath)));
} }
fvo = new FileVO(); fvo = new FileVO();
fvo.setFileExtsn(fileExt); fvo.setFileExtsn(fileExt);
//fvo.setFileStreCours(storePathString);
fvo.setFileMg(Long.toString(size)); fvo.setFileMg(Long.toString(size));
fvo.setOrignlFileNm(orginFileName); fvo.setOrignlFileNm(orginFileName);
fvo.setStreFileNm(newName); fvo.setStreFileNm(newName);
//fvo.setAtchFileId(atchFileIdString);
fvo.setFileSn(String.valueOf(fileKey)); fvo.setFileSn(String.valueOf(fileKey));
result.add(fvo); result.add(fvo);
@ -172,25 +205,43 @@ public class FileUploadController {
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
json = null; json = null;
e.printStackTrace(); e.printStackTrace();
ErrorMessage errorMessage = new ErrorMessage("ERROR", "JSON 변환 처리에 실패하였습니다 : " + e.toString());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
} }
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
//responseHeaders.set("Content-Type", "application/json; charset=UTF-8");
ResponseEntity ret = ResponseEntity.ok().headers(responseHeaders).body(json); ResponseEntity ret = ResponseEntity.ok().headers(responseHeaders).body(json);
return ret; return ret;
} }
/**
* 파일 다운로드를 처리한다.
*
* @param params
* @param mv
* @return
* @throws Exception
*/
@RequestMapping(value="/fileupload/downloadFiles.do") @RequestMapping(value="/fileupload/downloadFiles.do")
public ModelAndView download(@RequestParam HashMap<Object, Object> params, ModelAndView mv) { public ModelAndView download(@RequestParam HashMap<Object, Object> params, ModelAndView mv) throws Exception {
// TODO 권한 설정 기능 추가 필요
String fid = (String) params.get("fid"); String fid = (String) params.get("fid");
String fname = (String) params.get("fname"); String fname = (String) params.get("fname");
String subPath = nlibProperty.getProperty((String) params.get("subPathKey")); String subPath = nlibProperty.getProperty((String) params.get("subPathKey"));
String fullPath = FILEUPLOAD_BASE_PATH + "/" + subPath + "/" + fid; String fullPath = FILEUPLOAD_BASE_PATH + "/" + subPath + "/" + fid;
File file = new File(fullPath); if(FileUtil.isNotValid(fullPath)) {
throw new Exception(MSG_NOT_VALID_FILE_PATH);
}
mv.setViewName("downloadView"); File file = new File(FileUtil.filePathBlackList(fullPath));
if(!file.isFile()) {
throw new Exception("파일 정보가 부적합합니다. 관리자에게 문의하여 주시기 바랍니다.");
}
mv.setViewName("downloadView"); // dispatcher-servlet.xml내 BeanNameViewResolver 정의
mv.addObject("downloadFile", file); mv.addObject("downloadFile", file);
mv.addObject("fname", fname); mv.addObject("fname", fname);

View File

@ -1,5 +1,6 @@
package nlib.util; package nlib.util;
import egovframework.com.cmm.EgovWebUtil;
import egovframework.com.utl.fcc.service.EgovStringUtil; import egovframework.com.utl.fcc.service.EgovStringUtil;
/** /**
@ -50,4 +51,13 @@ public class FileUtil extends EgovStringUtil {
return !isValid(pathOrFilename); return !isValid(pathOrFilename);
} }
/**
* 파일 경로 문자열에서 보안 문제가 되는 문자를 제거하여 리턴한다.
*
* @param path
* @return
*/
public static String filePathBlackList(String path) {
return EgovWebUtil.filePathBlackList(path);
}
} }

View File

@ -82,4 +82,12 @@
<!-- /For Pagination Tag --> <!-- /For Pagination Tag -->
<!-- <mvc:view-controller path="/cmmn/validator.do" view-name="cmmn/validator"/> --> <!-- <mvc:view-controller path="/cmmn/validator.do" view-name="cmmn/validator"/> -->
<!-- 파일다운로드 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0"/>
</bean>
<bean id="downloadView" class="nlib.cmm.fileupload.DownloadView"/>
</beans> </beans>