Today
-
Yesterday
-
Total
-
  • Spring boot MultipartFile 확장자 알아내기
    Spring Boot 🍃 2023. 12. 3. 00:01

    spring boot 에서

    파일 업로드가 붙어있는 게시글을 등록 / 수정하는 페이지에서

    파일 업로드를 새로 하지 않았을 경우

    기존의 파일명을 가지고 신규 파일 등록을 해버리는 이슈가 발생해서 정리하게 되었다.


    다양한 방법이 있겠으나

    나의 경우 파일 업로드시엔 파일 확장자가 붙은 이름이 올라오고 파일을 업로드 하지 않은 경우 파일 확장자가 붙어있지 않은 파일 이름만 올라오는 경우가 생겼기 때문에

    확장자가 안붙어있으면 파일 업로드를 하지 않는것으로 처리하려고 하기 위해서

    파일 확장자를 검사하는 코드를 구해보았다.


    보고 배운 블로그 : wildeveloperetrain

    public String add(HttpServletRequest request, MultipartFile file, BannerInput parameter) {
    
        if (file != null) {
            String extension = StringUtils.getFilenameExtension(file.getOriginalFilename());
            if (extension != null) {
                FileUtil files = new FileUtil();
                String imgPath = files.save(file).getUrlFilePath();
                parameter.setImgPath(imgPath);
            }
        }
    }

    이 아래에 해당하는 코드가 파일의 확장자를 검사하는 코드이다.

    String extension = StringUtils.getFilenameExtension(file.getOriginalFilename());

    'Spring Boot 🍃' 카테고리의 다른 글

    스크래핑 Scraping  (0) 2023.12.04
    Spring Boot MariaDB 연결  (0) 2023.12.03
    Spring Boot 이미지 html에 출력하기  (0) 2023.12.03
    Thymeleaf  (0) 2023.12.03
    MyBatis  (0) 2023.12.03

Designed by Tistory / Custom by 얼거스