HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🧚
[1기]최종 프로젝트 데브코스
/
📜
[팀13] 사각사각 ✏️
/
🔥
트러블슈팅
/
⛏️
이미지 파일이 null 일때 처리
⛏️

이미지 파일이 null 일때 처리

 
트러블 슈팅이 일어나게 된 원인 : 현재 서비스에서 기본이미지로 바꿀 경우 프론트에서 null을 보내준다. 따라서 이미지 파일에 null 이 들어온다면 return null을 하고 아닐 경우는 S3에 이미지를 업로드 한다.
 

처음코드

String profileKey = image.map(imageFile -> { String key = User.class.getSimpleName() .toLowerCase() + "s" + "/" + userId.toString() + "/profile/" + UUID.randomUUID() + this.s3Uploader.getExtension(imageFile); return this.s3Uploader.upload( Bucket.IMAGE, imageFile, key, S3Uploader.imageExtensions ); } ) .orElse(null);
 

수정코드

String profileKey = image.map(imageFile -> { //추가 if (imageFile.isEmpty()) { return null; } String key = User.class.getSimpleName() .toLowerCase() + "s" + "/" + userId.toString() + "/profile/" + UUID.randomUUID() + this.s3Uploader.getExtension(imageFile); return this.s3Uploader.upload( Bucket.IMAGE, imageFile, key, S3Uploader.imageExtensions ); } ) .orElse(null);
 
MultipartFile로 이미지를 받을 때 파일을 보내지 않으면 null값으로 예상했지만,
null값이 아니였음!
따라서 .isEmty() 로 처리를 해줘야 한다.
Null check for multipart file
Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience. To learn more, see our tips on writing great answers.
Null check for multipart file
https://stackoverflow.com/questions/46934460/null-check-for-multipart-file/46966188
Null check for multipart file