재커밋

This commit is contained in:
KNKIM 2021-07-26 10:28:56 +09:00
parent 868ddde0aa
commit da6920107b
2 changed files with 227 additions and 227 deletions

View File

@ -1,186 +1,186 @@
package nlib.cmm.fileupload; package nlib.cmm.fileupload;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import egovframework.com.cmm.EgovWebUtil; import egovframework.com.cmm.EgovWebUtil;
import egovframework.com.cmm.service.FileVO; import egovframework.com.cmm.service.FileVO;
import nlib.cmm.exception.ErrorMessage; import nlib.cmm.exception.ErrorMessage;
import nlib.cmm.service.NlibProperty; import nlib.cmm.service.NlibProperty;
import nlib.util.FileUtil; import nlib.util.FileUtil;
import nlib.util.StringUtil; import nlib.util.StringUtil;
import nlib.util.UUID; import nlib.util.UUID;
/** /**
* <pre> * <pre>
* @Class Name : FileUploadController.java * @Class Name : FileUploadController.java
* *
* @Description : 파일 업로드를 처리하는 컨트롤러 * @Description : 파일 업로드를 처리하는 컨트롤러
* *
* *
* @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021) * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
* *
* </pre> * </pre>
* *
* @ ------------ -------- --------------------------- * @ ------------ -------- ---------------------------
* @ 수정일 수정자 수정내용 * @ 수정일 수정자 수정내용
* @ ------------ -------- --------------------------- * @ ------------ -------- ---------------------------
* @ 2021. 7. 15. KNKIM 최초 생성 * @ 2021. 7. 15. KNKIM 최초 생성
* *
* *
* @author 이씨플라자 * DIGITALSHIP KNKIM * @author 이씨플라자 * DIGITALSHIP KNKIM
* @since 2021. 7. 15. * @since 2021. 7. 15.
* @version 1.0 * @version 1.0
* *
*/ */
@Controller @Controller
public class FileUploadController { public class FileUploadController {
private static final Logger log = LoggerFactory.getLogger(FileUploadController.class); private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);
@Resource(name="nlibProperty") @Resource(name="nlibProperty")
NlibProperty nlibProperty; NlibProperty nlibProperty;
/** /**
* 첨부파일이 저장되는 최상위 위치 * 첨부파일이 저장되는 최상위 위치
*/ */
@Value("#{properties['fileupload.base.path']}") @Value("#{properties['fileupload.base.path']}")
private String FILEUPLOAD_BASE_PATH; private String FILEUPLOAD_BASE_PATH;
/** /**
* 첨부파일 임시 저장 위치 * 첨부파일 임시 저장 위치
*/ */
@Value("#{properties['fileupload.temp.subpath']}") @Value("#{properties['fileupload.temp.subpath']}")
private String FILEUPLOAD_TEMP_SUBPATH; private String FILEUPLOAD_TEMP_SUBPATH;
@ResponseBody @ResponseBody
@RequestMapping(value="/fileupload/uploadFilesAjax.do", produces="application/json") @RequestMapping(value="/fileupload/uploadFilesAjax.do", produces="application/json")
public ResponseEntity uploadFilesAjax( public ResponseEntity uploadFilesAjax(
final MultipartHttpServletRequest multiRequest final MultipartHttpServletRequest multiRequest
, @RequestParam("subPathKey") String subPathKey , @RequestParam("subPathKey") String subPathKey
, HttpServletResponse response) { , HttpServletResponse response) {
// TODO : 권한 체크 추가할 // 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.isNotValid(FILEUPLOAD_BASE_PATH)) throw new Exception("첨부파일이 저장되는 최상위 위치값이 적절하지 않습니다.");
// 서브 위치 // 서브 위치
if(StringUtil.isEmpty(subPathKey)) throw new Exception("첨부파일이 저장되는 위치정보값이 정확하지 않습니다."); if(StringUtil.isEmpty(subPathKey)) throw new Exception("첨부파일이 저장되는 위치정보값이 정확하지 않습니다.");
String subPath = nlibProperty.getProperty(subPathKey); String subPath = nlibProperty.getProperty(subPathKey);
if(FileUtil.isNotValid(subPath)) throw new Exception("첨부파일 서브 위치값이 적절하지 않습니다."); if(FileUtil.isNotValid(subPath)) throw new Exception("첨부파일 서브 위치값이 적절하지 않습니다.");
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();
if(!files.isEmpty()) { if(!files.isEmpty()) {
Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator(); Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();
MultipartFile file; MultipartFile file;
String filePath = ""; String filePath = "";
FileVO fvo; FileVO fvo;
int fileKey = 1; int fileKey = 1;
while (itr.hasNext()) { while (itr.hasNext()) {
Entry<String, MultipartFile> entry = itr.next(); Entry<String, MultipartFile> entry = itr.next();
file = entry.getValue(); file = entry.getValue();
String orginFileName = file.getOriginalFilename(); String orginFileName = file.getOriginalFilename();
//-------------------------------------- //--------------------------------------
// 파일명이 없는 경우 처리 // 파일명이 없는 경우 처리
// (첨부가 되지 않은 input file type) // (첨부가 되지 않은 input file type)
//-------------------------------------- //--------------------------------------
if ("".equals(orginFileName)) { if ("".equals(orginFileName)) {
continue; continue;
} }
////------------------------------------ ////------------------------------------
int index = orginFileName.lastIndexOf("."); int index = orginFileName.lastIndexOf(".");
//String fileName = orginFileName.substring(0, index); //String fileName = orginFileName.substring(0, index);
String fileExt = orginFileName.substring(index + 1); String fileExt = orginFileName.substring(index + 1);
//String newName = KeyStr + getTimeStamp() + fileKey; //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); 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(EgovWebUtil.filePathBlackList(filePath)));
} }
fvo = new FileVO(); fvo = new FileVO();
fvo.setFileExtsn(fileExt); fvo.setFileExtsn(fileExt);
//fvo.setFileStreCours(storePathString); //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.setAtchFileId(atchFileIdString);
fvo.setFileSn(String.valueOf(fileKey)); fvo.setFileSn(String.valueOf(fileKey));
result.add(fvo); result.add(fvo);
fileKey++; fileKey++;
} }
} }
} catch(Exception e) { } catch(Exception e) {
ErrorMessage errorMessage = new ErrorMessage("ERROR", "처리에 실패하였습니다 : " + e.toString()); ErrorMessage errorMessage = new ErrorMessage("ERROR", "처리에 실패하였습니다 : " + e.toString());
e.printStackTrace(); e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
} }
// Convert Json // Convert Json
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
String json; String json;
try { try {
json = mapper.writeValueAsString(result); json = mapper.writeValueAsString(result);
log.debug("RETURN DATA > json:" + json); log.debug("RETURN DATA > json:" + json);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
json = null; json = null;
e.printStackTrace(); e.printStackTrace();
} }
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
//responseHeaders.set("Content-Type", "application/json; charset=UTF-8"); //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;
} }
} }

View File

@ -1,41 +1,41 @@
package nlib.cmm.fileupload; package nlib.cmm.fileupload;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
public class FileUploadDomain implements Serializable { public class FileUploadDomain implements Serializable {
private String name; private String name;
private String description; private String description;
private List<MultipartFile> images; private List<MultipartFile> images;
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public String getDescription() { public String getDescription() {
return description; return description;
} }
public void setDescription(String description) { public void setDescription(String description) {
this.description = description; this.description = description;
} }
public List<MultipartFile> getImages() { public List<MultipartFile> getImages() {
return images; return images;
} }
public void setImages(List<MultipartFile> images) { public void setImages(List<MultipartFile> images) {
this.images = images; this.images = images;
} }
} }