Tiny Bunny
본문 바로가기
Java

Java - 파일 업로드(1)

by 내이름효주 2024. 4. 25.
  • 파일명 변경
    • 현재는 1,2,3,4 ··· 이런식으로 파일명이 업로드되는데 나는 다운로드시에 저장된 파일명으로 저장할것이다!
    • public String getFileName() {
      		
      		String GenFileName[] = originFileName.split(fileExt);
      		String NewGenFileName = GenFileName[0].substring(0, GenFileName[0].length()-1);
      		
      		return id + "_" + NewGenFileName + "." + fileExt;
      	}
      ➡ 기존 파일명은 확장자까지 저장하기 때문에 확장자(fileExt)로 나눠서 저장➡ 근데 그렇게 저장하면 마지막에 .까지 나오니까 마지막 전까지만 파일명 저장
      그리고 정렬하기 좋게 id를 앞에 붙여서 파일명 저장
  • 파일 저장 경로
    • // 새 파일이 저장될 폴더(io파일) 객체 생성
      String targetDirPath = genFileDirPath + "/" + relTypeCode + "/" + fileDir;
      java.io.File targetDir = new java.io.File(targetDirPath);
      ➡ 파일이 저장될 폴더를 생성해준다
      ➡ relTypeCode로 분류해서 생성! 그래서 form 보낼때 relTypeCode를 지정해줌

 

🚨새로운 오류🚨

There was an unexpected error (type=Internal Server Error, status=500).
Current request is not of type [org.springframework.web.multipart.MultipartRequest]: 
ServletWebRequest: uri=/usr/contactUs/joinapplication;client=0:0:0:0:0:0:0:1;session=F37A2D12EAABC7FA277CDB26D6542F26
java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartRequest]: ServletWebRequest: uri=/usr/contactUs/joinapplication;client=0:0:0:0:0:0:0:1;session=F37A2D12EAABC7FA277CDB26D6542F26 at org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.resolveNativeRequest(ServletRequestMethodArgumentResolver.java:131) at org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.resolveArgument(ServletRequestMethodArgumentResolver.java:120) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:146) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:887) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798)

 

  •  [GPT 답변]
    문제는 /usr/contactUs/joinapplication 엔드포인트에서 MultipartRequest를 파라미터로 받고자 하지만, 실제 요청은 멀티파트 요청이 아닌 것으로 감지되었다는 것
    일반적으로 MultipartRequest는 Spring MVC에서 멀티파트 요청을 처리할 때 사용됩니다. 그러나 이것은 Spring Boot와 Spring MVC의 과거 버전에서 사용되었던 방식이며, 요즘에는 대부분 MultipartFile 또는 HttpServletRequest를 사용하여 멀티파트 요청을 처리합니다.
    따라서 해결 방법은 다음과 같습니다.
    1. MultipartRequest 대신에 MultipartFile 또는 HttpServletRequest를 사용하세요.
    2. 컨트롤러 메서드 시그니처를 변경하여 MultipartRequest를 사용하지 않도록 합니다.
    3. 클라이언트 요청 확인: 클라이언트가 올바른 요청을 보내는지 확인하십시오. 파일 업로드와 같은 경우 HTML 폼에 enctype="multipart/form-data" 속성이 있는지 확인하십시오.
    4. 컨트롤러 메서드 수정: 컨트롤러 메서드의 시그니처를 수정하여 멀티파트 요청을 처리할 수 있도록 파라미터를 추가하십시오. 예를 들어, HttpServletRequest 또는 MultipartFile 파라미터를 추가할 수 있습니다.
    5. 멀티파트 구성 확인: 스프링 구성이 멀티파트 요청을 처리할 수 있도록 제대로 구성되었는지 확인하십시오. 필요한 경우 MultipartResolver 빈을 설정하십시오.