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

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

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

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


2. war 파일 첨부

SpringFileUpload.war


3. 코딩 소스

// 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"; // 리스트 요청으로 보내야하는데 일단 제외하고 구현
	}
}

// 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;
	}
}






	
	
	
		
		
	
	
	

 





  SpringBoard
  
  
  	encodingFilter
  	org.springframework.web.filter.CharacterEncodingFilter
  	
  		encoding
  		UTF-8
  	
  
  
  
  	encodingFilter
  	/*
  
  
  
  	dispatcher
  	org.springframework.web.servlet.DispatcherServlet
  	
  		contextConfigLocation
  			/WEB-INF/beans.xml
  	
  
  
  
  	dispatcher
  	*.do
  


<!-- fileForm.jsp -->

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="file.do" method="post" enctype="multipart/form-data">
		<fieldset>
			<table>
				<tr>
					<th>이름</th>
					<td><input type="text" name="name" required="required" placeholder="이름"></td>
				</tr>
				<tr>
					<th>비밀번호</th>
					<td><input type="password" name="pwd" required="required" placeholder="비밀번호"></td>
				</tr>
				<tr>
					<th>파일</th>
					<td><input type="file" name="uploadfile" required="required"></td>
				</tr>
				<tr>
					<th>제목</th>
					<td><input type="text" name="title" required="required" placeholder="제목"></td>
				</tr>
				<tr>
					<th>내용</th>
					<td><textarea rows="20" cols="40" name="content" required="required" placeholder="내용"></textarea></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" value="작성">
						<input type="reset" value="취소">
					</td>
				</tr>
			</table>
		</fieldset>
	</form>
</body>
</html>


4. 실행 화면

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


+ Recent posts