//controller
/*
* 업로드
*/
/**
* File upload.
*
* @param req the req
* @param res the res
* @param uploadFile the upload file
* @return the response entity
*/
@PostMapping(value={Url.COMMON.FILE_UPLOAD})
@ResponseBody
public ResponseEntity<Object> fileUpload(HttpServletRequest req, HttpServletResponse res, MultipartFile uploadFile) {
Map<String, Object> resultMap = new HashMap<>();
FileInfo _file;
try {
_file = fileService.uploadFile(req);
if(_file == null || isEmpty(_file.getFileId())) {
resultMap.put("result", "FAIL");
} else {
resultMap.put("result", "SUCC");
resultMap.put("fileInfo", _file);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return makeResponseEntity(resultMap);
}
fileService.java
/**
* Upload file.
*
* @param req the req
* @return the file info
* @throws IOException Signals that an I/O exception has occurred.
*/
public FileInfo uploadFile(HttpServletRequest req) throws IOException;
fileServiceImpl.java
@Override
public FileInfo uploadFile(HttpServletRequest req) throws IOException {
FileInfo result = new FileInfo();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
// Upload 경로에 디렉토리가 없을 경우 만듬
Path uploadDir = Paths.get(UPLOAD_FILE_PATH);
// FIXME file type cd 에 따른 업로드 디렉토리 구분처리
// FIXME file type이 이미지인 경우 thum 이미지 생성 처리 추가
// FIXME file group id를 사용할 경우에 대한 처리
// 업로드 디렉도리가 존재하지 않거나 디렉도리가 아닐경우 생성합니다.
if(!Files.isDirectory(uploadDir)) {
Files.createDirectories(uploadDir);
}
for(MultipartFile mFile : multipartRequest.getFileMap().values()) {
UUID tempFileName = UUID.randomUUID();
String originalFileName = mFile.getOriginalFilename(); // 원본 파일명
String fileExt = FilenameUtils.getExtension(originalFileName);
if(originalFileName.toLowerCase().endsWith(".tar.bz2")) {
fileExt = "tar.bz2";
} else if(originalFileName.toLowerCase().endsWith(".tar.gz")) {
fileExt = "tar.gz";
}
// 물리파일은 확장자를 제외하고 저장한다.
String logicalFileName = tempFileName.toString()+ "." + fileExt;
//File file = new File(uploadDir + "/" + logicalFileName);
//transferTo(mFile, file);
byte[] fileBytes = mFile.getBytes();
Path filePath = uploadDir.resolve(logicalFileName);
Files.write(filePath, fileBytes);
// 사이즈가 0일 경우 파일이 존재하지 않는 것으로 판단.
/*if( mFile.getSize() == 0 ) {
log.warn("Abort upload file. empty fle");
throw new RuntimeException("Abort upload file. empty fle");
}*/
result.setPyscPath(uploadDir.toString());
result.setPyscNm(logicalFileName);
result.setOrigNm(originalFileName);
result.setFileExt(fileExt);
result.setContentType(mFile.getContentType());
result.setFileSize(mFile.getSize());
result.setRegId(loginUserName());
fileMapper.insertFile(result);
if(isEmpty(result.getFileId())) {
throw new RuntimeException("Unable to Save File Info : An error occurred while file upload.");
}
break;
}
return result;
}
fileMapper.java
/**
* Insert file.
*
* @param file the file
* @return the int
*/
int insertFile(FileInfo file);
fileMapper.xml
<insert id="insertFile" parameterType="fileInfo" useGeneratedKeys="true" keyProperty="fileId">
INSERT
INTO
TB_FILE
(
<!-- FILE_GRP_ID -->
ORIG_NM
,PYSC_NM
,THUMB_NM
,PYSC_PATH
,THUMB_PATH
,FILE_EXT
,FILE_SIZE
,CONTENT_TYPE
,USE_YN
,REG_ID
)
VALUES(
<!-- #{fileGrpId} -->
#{origNm}
,#{pyscNm}
,#{thumbNm}
,#{pyscPath}
,#{thumbPath}
,#{fileExt}
,#{fileSize}
,#{contentType}
,'Y'
,#{regId}
)
</insert>