강의

멘토링

커뮤니티

Inflearn Community Q&A

aectarine1507's profile image
aectarine1507

asked

Spring MVC Part 2 - Backend Web Development Utilization Technology

Implementing File Upload and Download with Examples

attachFile에서 텍스트파일 업로드시 null로 반환되는 문제

Resolved

Written on

·

331

0

[질문 내용]

안녕하세요.

 

첫번째 첨부파일에 텍스트 파일을 업로드하면

 

null이 발생하고, 이미지를 넣으면 정상 작동하는데

 

어떤 문제인지 잘 모르겠습니다.

 

@PostMapping("/items/new")
public String saveItem(@ModelAttribute ItemForm form, RedirectAttributes redirectAttributes) throws IOException {

    log.info("Received Form: {}", form);

    log.info("AttachFile: {}", form.getAttachFile());

    // 업로드 파일
    UploadFile attachFile = fileStore.storeFile(form.getAttachFile());
    log.info("attachFile = {}", attachFile);

    // 업로드 이미지
    List<UploadFile> storeImageFiles = fileStore.storeFiles(form.getImageFiles());
image.pngimage.png

 

 

springmvc

Answer 2

1

안녕하세요. ㅎ님, 공식 서포터즈 y2gcoder입니다.

댓글에 말씀해주신대로 텍스트 파일에 내용이 없으면, 다시 말해 텍스트 파일의 크기가 0 이면 문제가 발생하는 것이 맞습니다!

public UploadFile storeFile(MultipartFile multipartFile) throws IOException {
  if (multipartFile.isEmpty()) {
    return null;
  }

  String originalFilename = multipartFile.getOriginalFilename();
  String storeFileName = createStoreFileName(originalFilename);
  multipartFile.transferTo(new File(getFullPath(storeFileName)));
  return new UploadFile(originalFilename, storeFileName);
}

FileStorestoreFile() 에서 multipartFile.isEmpty() 내부를 보면 파일의 사이즈가 0인지를 확인하고 있기 때문입니다!

image.png

위의 캡처는 MultipartFile 의 구현체 코드를 예시르 보여드렸습니다!

 

감사합니다.

aectarine1507님의 프로필 이미지
aectarine1507
Questioner

감사합니다 ^^

0

aectarine1507님의 프로필 이미지
aectarine1507
Questioner

텍스트 파일에 내용이 없으면 발생하는 문제로 확인되었습니다.

원인이 무엇일까요

aectarine1507's profile image
aectarine1507

asked

Ask a question