재커밋

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

View File

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