Tiny Bunny
본문 바로가기
프로젝트/Team

Java - 파일 업로드(2)

by 내이름효주 2024. 4. 25.

 

🚨 문제🚨

기존 genfile table에는 relTypeCode와 relId가 필요 이걸 현재 폼에는 없는 값이라 어떻게 처리해줄지?

 

  • 연주자 가입신청에만 파일 업로드가 필요해서 파일업로드하는 controller에서 relTypeCode를 지정해줘
    String relTypeCode = "application";
  • relId는 현재 가입하는 연주자의 id를 의미
    @Select("""
            SELECT MAX(id) + 1
            FROM artist;
            """)
    public int getCurrentPressId();​
     
    🚧 근데~ 여기서 사고났어;;🚧
    Mapper method 'com.example.demo.repository.ContactUsRepository.getCurrentPressId' attempted to return null from a method with a primitive return type (int).​
     ➡ 이유는? 테이블에 값이 하나도 없으니까 id가 null 상태 인거지!!!
    해결방법!은 간단할듯 null일 때 id = 0주면 되겠지?
    @Select("""
            SELECT IFNULL(MAX(id) + 1,0)
            FROM artist;
            """)
    public int getCurrentPressId();​
  • 이제 위에서 구한 id를 jsp에 보내주고 가입신청하고 id를 넣어서 submit하면 id를 같이 넘겨주면서 파일명을 정해줌
    (파일을 저장할 때 map으로 저장하는데 key가 될 애라서 필요해!!, value는 multipartRequest)
    (그리고 파일 저장은 relTypeCode명의 폴더가 추가가 되고 그 안에 저장됨!! )
    // 새 파일이 저장될 폴더(io파일) 객체 생성
    String targetDirPath = genFileDirPath + "/" + relTypeCode + "/" + fileDir;
    java.io.File targetDir = new java.io.File(targetDirPath);​