/ LECTURE

Spring - File Upload

이 페이지는 다음에 대한 공부 기록입니다

JAVA(자바), Python(파이썬) 기반의

AI 활용 응용 소프트웨어 개발자 양성 과정

2021.11.10. ~ 2022.05.18.

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

64일차 수업

파일 업로드

View 에서 파일 받기

<td bgcolor="orange" width="70">파일 추가</td>
<td><input type="file" name="file" maxlength="60" size="40"/></td>

VO 에서 파일 저장하기

private String b_fname;
private String b_fname_en;
private long b_fsize;

//*************************************************
MultipartFile file;    // write.jsp에 파일 첨부시 name="file"과 동일한 변수명

public MultipartFile getFile() {
    return file;
}

public void setFile(MultipartFile file) {
    this.file = file;
    // 업로드 파일 접근
    if(! file.isEmpty()){
        this.b_fname = file.getOriginalFilename();
        this.b_fsize = file.getSize();
        // 가짜 이름도 하나 생성
        // 파일 확장자 진짜이름에서 가져오기
        String fileExtension = b_fname.substring(b_fname.lastIndexOf("."));
        this.b_fname_en = UUID.randomUUID().toString().replaceAll("-","")+fileExtension;

//***********************************************
        // 해당 경로로 변경
        File f = new File("파일이 저장될 절대경로"+b_fname_en);
        try {
             file.transferTo(f); 
        } catch (IllegalStateException e) {             
             e.printStackTrace();
        } catch (IOException e) {
             e.printStackTrace();
        }
    }
}