개발자의 끄적끄적

스프링(Spring) 파일 업로드(File Upload) [펌] 본문

개발/java & jsp

스프링(Spring) 파일 업로드(File Upload) [펌]

효벨 2020. 8. 10. 02:00
728x90
반응형

스프링(Spring) 파일 업로드(File Upload) [펌]

 

1. 스프링(Spring) 파일 업로드(File Upload)

- 필요 jar 파일 (com.springsource.org.apache.commons.fileupload.jar, com.springsource.org.apache.commons.io.jar)

- HTML

태그의 enctype 속성을 multipart/form-data 설정

- 스프링 설정 파일에 MultipartResolver 설정

 

2. 코딩 소스

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

// FileController.java

 

package controller.com.tistory.gangzzang;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

 

import model.com.tistory.gangzzang.FileDTO;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.servlet.ModelAndView;

 

@Controller

public class FileController {

 

  @RequestMapping(value = "/file.do", method = RequestMethod.GET)

  public ModelAndView fileForm() {

    ModelAndView mv = new ModelAndView();

    mv.setViewName("fileForm");

    return mv;

  }

   

  @RequestMapping(value = "/file.do", method = RequestMethod.POST)

  public String fileSubmit(FileDTO dto) {

    MultipartFile uploadfile = dto.getUploadfile();

    if (uploadfile != null) {

      String fileName = uploadfile.getOriginalFilename();

      dto.setFileName(fileName);

      try {

        // 1. FileOutputStream 사용

        // byte[] fileData = file.getBytes();

        // FileOutputStream output = new FileOutputStream("C:/images/" + fileName);

        // output.write(fileData);

         

        // 2. File 사용

        File file = new File("C:/images/" + fileName);

        uploadfile.transferTo(file);

      } catch (IOException e) {

        e.printStackTrace();

      } // try - catch

    } // if

    // 데이터 베이스 처리를 현재 위치에서 처리

    return "redirect:getBoardList.do"; // 리스트 요청으로 보내야하는데 일단 제외하고 구현

  }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

// FileDTO.java

 

package model.com.tistory.gangzzang;

 

import org.springframework.web.multipart.MultipartFile;

 

public class FileDTO {

  private String name, pwd, title, content, fileName;

  private MultipartFile uploadfile;

 

  public String getName() {

    return name;

  }

 

  public void setName(String name) {

    this.name = name;

  }

 

  public String getPwd() {

    return pwd;

  }

 

  public void setPwd(String pwd) {

    this.pwd = pwd;

  }

 

  public String getTitle() {

    return title;

  }

 

  public void setTitle(String title) {

    this.title = title;

  }

 

  public String getContent() {

    return content;

  }

 

  public void setContent(String content) {

    this.content = content;

  }

 

  public String getFileName() {

    return fileName;

  }

 

  public void setFileName(String fileName) {

    this.fileName = fileName;

  }

 

  public MultipartFile getUploadfile() {

    return uploadfile;

  }

 

  public void setUploadfile(MultipartFile uploadfile) {

    this.uploadfile = uploadfile;

  }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<!--?xml version="1.0" encoding="UTF-8"?-->

 

<!-- beans.xml -->

 

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="

  http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans.xsd

  http://www.springframework.org/schema/context

  http://www.springframework.org/schema/context/spring-context.xsd

  ">

 

  <context:component-scan base-package="controller.com.tistory.gangzzang">

   

  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/WEB-INF/view/">

    <property name="suffix" value=".jsp">

  </property></property></bean>

   

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

</bean></context:component-scan></beans>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

<!--?xml version="1.0" encoding="UTF-8"?-->

 

<!-- web.xml -->

 

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>SpringBoard</display-name>

   

  <filter>

    <filter-name>encodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>UTF-8</param-value>

    </init-param>

  </filter>

   

  <filter-mapping>

    <filter-name>encodingFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

   

  <servlet>

    <servlet-name>dispatcher</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

      <param-name>contextConfigLocation</param-name>

      <param-value> /WEB-INF/beans.xml</param-value>

    </init-param>

  </servlet>

   

  <servlet-mapping>

    <servlet-name>dispatcher</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

 

</web-app>

3. 실행 화면

- localhost:웹컨테이너포트번호/SpringFileUpload/file.do 요청으로 실행한다.

출처:https://gangzzang.tistory.com/entry/스프링Spring-파일-업로드File-Upload[갱짱.study]

 

반응형
Comments