https://developer-talk.tistory.com/811
[Java]MultipartFile 파일 이름 가져오는 방법
MultipartFile 파일 이름 가져오는 방법 이번 포스팅은 MultipartFile 형식으로 파일 업로드했을 때, 파일 이름을 가져오는 방법을 소개합니다. getOriginalFilename 메서드 파일 이름을 가져오는 방법은 상당
developer-talk.tistory.com
기존
// 패키지 등록
@RequestMapping(value = "/addPackage", method = RequestMethod.POST)
public String addPackagePOST(@ModelAttribute Pack pack,
@RequestParam("packPreviewImgFile") MultipartFile packPreviewImgFile,
@RequestParam("packSlideImg1File") MultipartFile packSlideImg1File,
@RequestParam("packSlideImg2File") MultipartFile packSlideImg2File,
@RequestParam("packSlideImg3File") MultipartFile packSlideImg3File,
@RequestParam("packContentImg1File") MultipartFile packContentImg1File,
@RequestParam("packContentImg2File") MultipartFile packContentImg2File,
@RequestParam("packContentImg3File") MultipartFile packContentImg3File,
@RequestParam("packCalendarImgFile") MultipartFile packCalendarImgFile,
Model model, HttpSession session) throws IllegalStateException, IOException {
if(packPreviewImgFile.isEmpty() || packSlideImg1File.isEmpty() || packSlideImg2File.isEmpty()
|| packSlideImg3File.isEmpty() || packContentImg1File.isEmpty() || packContentImg2File.isEmpty()
|| packContentImg2File.isEmpty() || packContentImg3File.isEmpty() || packCalendarImgFile.isEmpty()) {
//이미지 파일이 업로드되지 않은 경우 처리
model.addAttribute("message","파일이 업로드되지 않았습니다.");
return "redirect:/package/";
}
//전달파일을 저장하기 위한 서버 디렉토리의 시스템 경로 반환
String uploadDirectory = context.getServletContext().getRealPath("/resources/assets/img/upload");
//서버 디렉토리에 업로드 처리되며 저장된 파일의 이름을 반환하여 Command 객체의 필드값 변경
String uploadPreview = UUID.randomUUID().toString()+"-"+packPreviewImgFile.getOriginalFilename();
pack.setPackPreviewImg(uploadPreview);
String uploadSlide1 = UUID.randomUUID().toString()+"-"+packSlideImg1File.getOriginalFilename();
pack.setPackSlideImg1(uploadSlide1);
String uploadSlide2 = UUID.randomUUID().toString()+"-"+packSlideImg2File.getOriginalFilename();
pack.setPackSlideImg2(uploadSlide2);
String uploadSlide3 = UUID.randomUUID().toString()+"-"+packSlideImg3File.getOriginalFilename();
pack.setPackSlideImg3(uploadSlide3);
String uploadContent1 = UUID.randomUUID().toString()+"-"+packContentImg1File.getOriginalFilename();
pack.setPackContentImg1(uploadContent1);
String uploadContent2 = UUID.randomUUID().toString()+"-"+packContentImg2File.getOriginalFilename();
pack.setPackContentImg2(uploadContent2);
String uploadContent3 = UUID.randomUUID().toString()+"-"+packContentImg3File.getOriginalFilename();
pack.setPackContentImg3(uploadContent3);
String uploadCalendar = UUID.randomUUID().toString()+"-"+packCalendarImgFile.getOriginalFilename();
pack.setPackCalendarImg(uploadCalendar);
//파일 업로드 처리 - 복붙해서 넣어주는게 아니라 서버에 넣어줌
packPreviewImgFile.transferTo(new File(uploadDirectory,uploadPreview));
packSlideImg1File.transferTo(new File(uploadDirectory,uploadSlide1));
packSlideImg2File.transferTo(new File(uploadDirectory,uploadSlide2));
packSlideImg3File.transferTo(new File(uploadDirectory,uploadSlide3));
packContentImg1File.transferTo(new File(uploadDirectory,uploadContent1));
packContentImg2File.transferTo(new File(uploadDirectory,uploadContent2));
packContentImg3File.transferTo(new File(uploadDirectory,uploadContent3));
packCalendarImgFile.transferTo(new File(uploadDirectory,uploadCalendar));
//테이블에 행 삽입
packageService.addPackage(pack);
return "redirect:/package/";
}
/* 패키지 상품 정보 수정 */
@RequestMapping(value = "/packinfoModify", method = RequestMethod.POST)
public String packinfoModify(Pack pack, RedirectAttributes rttr) {
int result = packageService.updatePackage(pack);
rttr.addFlashAttribute("modify_result", result);
return "redirect:/package/";
}
}
변경
// 패키지 등록
@RequestMapping(value = "/addPackage", method = RequestMethod.POST)
public String addPackagePOST(@ModelAttribute Pack pack,
@RequestParam("packPreviewImgFile") MultipartFile packPreviewImgFile,
@RequestParam("packSlideImg1File") MultipartFile packSlideImg1File,
@RequestParam("packSlideImg2File") MultipartFile packSlideImg2File,
@RequestParam("packSlideImg3File") MultipartFile packSlideImg3File,
@RequestParam("packContentImg1File") MultipartFile packContentImg1File,
@RequestParam("packContentImg2File") MultipartFile packContentImg2File,
@RequestParam("packContentImg3File") MultipartFile packContentImg3File,
@RequestParam("packCalendarImgFile") MultipartFile packCalendarImgFile,
Model model, HttpSession session) throws IllegalStateException, IOException {
if(packPreviewImgFile.isEmpty() || packSlideImg1File.isEmpty() || packSlideImg2File.isEmpty()
|| packSlideImg3File.isEmpty() || packContentImg1File.isEmpty() || packContentImg2File.isEmpty()
|| packContentImg2File.isEmpty() || packContentImg3File.isEmpty() || packCalendarImgFile.isEmpty()) {
//이미지 파일이 업로드되지 않은 경우 처리
model.addAttribute("message","파일이 업로드되지 않았습니다.");
return "redirect:/package/";
}
//전달파일을 저장하기 위한 서버 디렉토리의 시스템 경로 반환
String uploadDirectory = context.getServletContext().getRealPath("/assets/img/upload");
//서버 디렉토리에 업로드 처리되며 저장된 파일의 이름을 반환하여 Command 객체의 필드값 변경
String uploadPreview = UUID.randomUUID().toString()+"-"+Normalizer.normalize(packPreviewImgFile.getOriginalFilename(),Normalizer.Form.NFC);
pack.setPackPreviewImg(uploadPreview);
String uploadSlide1 = UUID.randomUUID().toString()+"-"+Normalizer.normalize(packSlideImg1File.getOriginalFilename(),Normalizer.Form.NFC);
pack.setPackSlideImg1(uploadSlide1);
String uploadSlide2 = UUID.randomUUID().toString()+"-"+Normalizer.normalize(packSlideImg2File.getOriginalFilename(),Normalizer.Form.NFC);
pack.setPackSlideImg2(uploadSlide2);
String uploadSlide3 = UUID.randomUUID().toString()+"-"+Normalizer.normalize(packSlideImg3File.getOriginalFilename(),Normalizer.Form.NFC);
pack.setPackSlideImg3(uploadSlide3);
String uploadContent1 = UUID.randomUUID().toString()+"-"+Normalizer.normalize(packContentImg2File.getOriginalFilename(),Normalizer.Form.NFC);
pack.setPackContentImg1(uploadContent1);
String uploadContent2 = UUID.randomUUID().toString()+"-"+Normalizer.normalize(packContentImg2File.getOriginalFilename(),Normalizer.Form.NFC);
pack.setPackContentImg2(uploadContent2);
String uploadContent3 = UUID.randomUUID().toString()+"-"+Normalizer.normalize(packContentImg3File.getOriginalFilename(),Normalizer.Form.NFC);
pack.setPackContentImg3(uploadContent3);
String uploadCalendar = UUID.randomUUID().toString()+"-"+Normalizer.normalize(packCalendarImgFile.getOriginalFilename(),Normalizer.Form.NFC);
pack.setPackCalendarImg(uploadCalendar);
//파일 업로드 처리 - 복붙해서 넣어주는게 아니라 서버에 넣어줌
packPreviewImgFile.transferTo(new File(uploadDirectory,uploadPreview));
packSlideImg1File.transferTo(new File(uploadDirectory,uploadSlide1));
packSlideImg2File.transferTo(new File(uploadDirectory,uploadSlide2));
packSlideImg3File.transferTo(new File(uploadDirectory,uploadSlide3));
packContentImg1File.transferTo(new File(uploadDirectory,uploadContent1));
packContentImg2File.transferTo(new File(uploadDirectory,uploadContent2));
packContentImg3File.transferTo(new File(uploadDirectory,uploadContent3));
packCalendarImgFile.transferTo(new File(uploadDirectory,uploadCalendar));
//테이블에 행 삽입
packageService.addPackage(pack);
return "redirect:/package/";
}
'학원 > 에러' 카테고리의 다른 글
mapper 못불러와서 해결 (root-context.xml) (0) | 2023.08.19 |
---|---|
Java compiler level does not match the version of the installed Java project facet. (0) | 2023.08.04 |
STS 응용 프로그램을 열 수 없습니다 (0) | 2023.08.04 |
sts3 오류 (0) | 2023.08.04 |
[Mac OS] Port 8080 was already in use (0) | 2023.08.04 |