일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- R1C3
- 숫자형식오류
- DoitSQL입문
- 자바 예외
- 페이지분석
- 크롤링
- 웹 브라우저 전쟁
- SQL
- 우아한테크
- html
- 크롤링 오류
- 웹브라우저 수용도
- DoitSQL
- 생성자
- dbms
- SQL입문
- DoIt
- 배열 예제
- HTML역사
- 키-값 데이터베이스
- 자바
- 함수 선언
- Doit입문SQL
- 배열 3요소
- 예외
- 숫자 형식
- 데이터베이스
- 예제
- 자바 오류
- 함수
Archives
- Today
- Total
반응형
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- R1C3
- 숫자형식오류
- DoitSQL입문
- 자바 예외
- 페이지분석
- 크롤링
- 웹 브라우저 전쟁
- SQL
- 우아한테크
- html
- 크롤링 오류
- 웹브라우저 수용도
- DoitSQL
- 생성자
- dbms
- SQL입문
- DoIt
- 배열 예제
- HTML역사
- 키-값 데이터베이스
- 자바
- 함수 선언
- Doit입문SQL
- 배열 3요소
- 예외
- 숫자 형식
- 데이터베이스
- 예제
- 자바 오류
- 함수
Archives
- Today
- Total
프로그래밍
[Spring] day69 : 파일 업로드 예제 본문
728x90
반응형
2023.08.17.목
파일업로드
프로그램의 수행 순서를 제어하거나, 문장들의 수행 횟수를 조정하는 문장
파일을 선택하여 전용 폴더에 복사본 생성하고 DB에 등록하는 예제를 실습해보자
파일업로드 예제 구현하기
pom.xml에 해당 의존성을 추가해준다
<!-- 파일 업로드 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<form> 태그에 파일을 인코딩할 수 있는 설정을 추가해준다
insertBoard.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글작성페이지</title>
</head>
<body>
<form action="insertBoard.do" method="post" enctype="multipart/form-data">
<input type="hidden" name="writer" value="${member}">
<input type="text" name="title" required placeholder="제목 작성"> <br>
<input type="text" name="content" required placeholder="내용 작성"> <br>
<input type="file" name="fileUpload"> <br>
<input type="submit" value="글 작성">
</form>
<hr>
<a href="main.do">메인으로 돌아가기</a>
</body>
</html>
파일에 관한 새로운 멤버변수를 추가해준다
BoardVO.java
package com.spring.biz.board;
import org.springframework.web.multipart.MultipartFile;
public class BoardVO {
private int bid;
private String title;
private String content;
private String writer;
private int cnt;
private String searchCondition;
private String searchContent;
private MultipartFile fileUpload;
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public MultipartFile getFileUpload() {
return fileUpload;
}
public void setFileUpload(MultipartFile fileUpload) {
this.fileUpload = fileUpload;
}
@Override
public String toString() {
return "BoardVO [bid=" + bid + ", title=" + title + ", content=" + content + ", writer=" + writer + ", cnt="
+ cnt + "]";
}
}
원래 형태
bVO.setFileUpload(new MultipartFile(req.getxxx()));
MultipartFile은 원시타입이 아닌 객체이기 때문에 new를 해야한다
Resolver는 DS의 멤버변수이므로 DispatcherServlet-servlet.xml에서 설정해주어야 한다
DispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-4.2.xsd">
<context:component-scan base-package="com.spring.view.controller" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="-1" />
</bean>
</beans>
Controller에 해당로직을 추가하였다
@RequestMapping(value="/insertBoard.do", method=RequestMethod.POST)
public String insertBoard(BoardVO bVO) throws IllegalStateException, IOException {
MultipartFile fileUpload=bVO.getFileUpload();
if(!fileUpload.isEmpty()){
String fileName=fileUpload.getOriginalFilename();
System.out.println("파일명: "+fileName);
bVO.setFileName(fileName);
fileUpload.transferTo(new File("D:\\KIM\\ws\\day60\\src\\main\\webapp\\images\\"+fileName));
}
else {
bVO.setFileName("JJANGJJANGJ.png");
}
if(boardService.insert(bVO)){
return "redirect:main.do";
}
else{
return "redirect:insertBoard.jsp";
}
}
파일명 중복을 우려하여 어플리케이션 전용 폴더를 별도로 생성 후
생성된 별도의 폴더에 파일을 복사하여 저장하였다
[글 작성 흐름]
☆ insertBoard.do 요청
-> 커맨드 객체 만들음 new
-> request 내장객체로부터 값 추출
-> new : 객체를 멤버변수로 사용하는 경우
=> 해당 객체에 맞는 Resolver 객체가 필요함!
-> setter 호출
-> C가 맞는 메서드를 수행
728x90
반응형
'자바 > Spring' 카테고리의 다른 글
[Spring] day70 : NULL update 이슈 (1) | 2023.08.20 |
---|---|
[Spring] day70 : 예외 발생 처리 페이지 (0) | 2023.08.18 |
[Spring] day68 : JDBC Template (0) | 2023.08.17 |
[Spring] day67 : AOP 예제(+바인드 변수) (0) | 2023.08.16 |
[Spring] day66 : AOP(관점 지향 프로그래밍) (1) | 2023.08.14 |
Comments