Notice
Recent Posts
Recent Comments
Link
개발 무지렁이
[Spring Boot] 결과보고서 RsData 본문
[dto.RsData.java]
@AllArgsConstructor
@Getter
@ToString
public class RsData<T> {
private final String resultCode;
private final String msg;
private final T body;
public boolean isSuccess() {
return resultCode.startsWith("S-");
}
public boolean isFail() {
return isSuccess() == false;
}
}
[GenFileService.java]
@Service
@RequiredArgsConstructor
public class GenFileService {
private final GenFileRepository genFileRepository;
public RsData<Map<String, GenFile>> saveFiles(Article article, Map<String, MultipartFile> fileMap) {
String relTypeCode = "article";
long relId = article.getId();
Map<String, GenFile> genFileIds = new HashMap<>();
for (String inputName : fileMap.keySet()) {
MultipartFile multipartFile = fileMap.get(inputName);
if (multipartFile.isEmpty()) {
continue;
}
String[] inputNameBits = inputName.split("__");
String typeCode = inputNameBits[0];
String type2Code = inputNameBits[1];
String originFileName = multipartFile.getOriginalFilename();
String fileExt = Util.file.getExt(originFileName);
String fileExtTypeCode = Util.file.getFileExtTypeCodeFromFileExt(fileExt);
String fileExtType2Code = Util.file.getFileExtType2CodeFromFileExt(fileExt);
int fileNo = Integer.parseInt(inputNameBits[2]);
int fileSize = (int) multipartFile.getSize();
String fileDir = relTypeCode + "/" + Util.date.getCurrentDateFormatted("yyyy_MM_dd");
GenFile genFile = GenFile
.builder()
.relTypeCode(relTypeCode)
.relId(relId)
.typeCode(typeCode)
.type2Code(type2Code)
.fileExtTypeCode(fileExtTypeCode)
.fileExtType2Code(fileExtType2Code)
.fileNo(fileNo)
.fileSize(fileSize)
.fileDir(fileDir)
.fileExt(fileExt)
.originFileName(originFileName)
.build();
genFileRepository.save(genFile);
String filePath = AppConfig.GET_FILE_DIR_PATH + "/" + fileDir + "/" + genFile.getFileName();
File file = new File(filePath);
file.getParentFile().mkdirs();
try {
multipartFile.transferTo(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
genFileIds.put(inputName, genFile);
}
return new RsData("S-1", "파일을 업로드했습니다.", genFileIds);
}
}
RsData (
resultCode=S-1,
msg=파일을 업로드했습니다.,
body={
common__bodyImg__2=GenFile(super=BaseEntity(id=2, createDate=2022-12-29T18:59:41.967930500, modifyDate=2022-12-29T18:59:41.967930500),
relTypeCode=article,
relId=1,
typeCode=common,
type2Code=bodyImg,
fileExtTypeCode=img,
fileExtType2Code=jpg,
fileSize=89449,
fileNo=2,
fileExt=jpg,
fileDir=article/2022_12_29,
originFileName=vintage-g8881ba0be_640.jpg),
common__bodyImg__1=GenFile(super=BaseEntity(id=1, createDate=2022-12-29T18:59:41.936012700, modifyDate=2022-12-29T18:59:41.936012700),
relTypeCode=article,
relId=1,
typeCode=common,
type2Code=bodyImg,
fileExtTypeCode=img,
fileExtType2Code=jpg,
fileSize=119736,
fileNo=1,
fileExt=jpg,
fileDir=article/2022_12_29,
originFileName=thomas-charters-zAi2Is48-MA-unsplash.jpg)
}
)
'Backend > 스프링부트' 카테고리의 다른 글
[Spring Boot] toastr을 이용해서 성공메세지 보여주기 (0) | 2022.12.30 |
---|---|
[Spring Boot] Util클래스에서 URL 한글 인코딩하기 (0) | 2022.12.30 |
[Spring Boot] 업로드된 파일의 메타정보 (0) | 2022.12.27 |
[Spring Boot] 때에 따라 업로드 개수가 다를 경우, MultipartRequest (0) | 2022.12.26 |
[Spring Boot] 프로필 이미지 업데이트와 캐시 (0) | 2022.12.25 |
Comments