일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 제이쿼리
- html
- css
- IPO
- 주식 청약
- SQL
- 6월 공모주 청약 일정
- Stock ipo
- 코드이그나이터
- 7월 공모주 청약 일정
- JavaScript
- 공모주
- 주식
- Oracle
- linux
- 자바스크립트
- 맥
- php
- Stock
- Eclipse
- 리눅스
- codeigniter
- 공모주 청약 일정
- MYSQL
- jquery
- 공모주 청약
- 주식 청약 일정
- 자바
- 오라클
- java
- Today
- Total
개발자의 끄적끄적
스프링(Spring) 파일 업로드(File Upload) [펌] 본문
스프링(Spring) 파일 업로드(File Upload) [펌]
1. 스프링(Spring) 파일 업로드(File Upload)
- 필요 jar 파일 (com.springsource.org.apache.commons.fileupload.jar, com.springsource.org.apache.commons.io.jar)
- HTML
- 스프링 설정 파일에 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]
'개발 > java & jsp' 카테고리의 다른 글
[java & spring] 문자열(String)의 빈 값 혹은 Null을 다루는 여러가지 방법[펌] (0) | 2020.08.11 |
---|---|
[Spring] ResponseEntity는 왜 쓰는 것이며 어떻게 쓰는걸까? [펌] (0) | 2020.08.11 |
[JSTL] jstl if else 문 [펌] (0) | 2020.08.05 |
[java/jsp] JSP 처리 중 Page directive: illegal to have multiple occurrences of contentType with different values [펌] (0) | 2020.08.05 |
[java/jsp] JSTL - <c:set> 태그 사용법 (0) | 2020.08.05 |