접기
package springmvc.fileup.ctr;
import java.io.File; import java.util.HashMap; import java.util.Iterator;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView;
import springmvc.fileup.dto.SingleUpDTO;
@Controller public class FileUploadController { private String uploadDirectory;//upload된 파일을 저장할 디렉토리 public FileUploadController(String uploadDirectory) {//최종 저장소가 안바뀐다는 가정하에 생성자에서 주입 받음 super(); this.uploadDirectory = uploadDirectory; } //파일을 받는 변수 앞에는 @RequestParam을 붙여준다. @RequestMapping("/singleUp.do") public ModelAndView singleUp(String comment,@RequestParam MultipartFile upfile)throws Exception{ boolean b = upfile.isEmpty();//false이면 업도드된 파일이 있다. HashMap map = new HashMap(); if(upfile!=null && !b){ System.out.println("-----------업로드된 파일 정보------------"); String fileName = upfile.getOriginalFilename(); long fileSize = upfile.getSize(); System.out.println("파일명 : "+fileName+", 크기 : "+fileSize); //파일 이동(임시저장소 upfile의 정보를 영구적 저장소 uploadDirectory로 보내준다) upfile.transferTo(new File(uploadDirectory,fileName)); map.put("fileName", fileName); } System.out.println("코멘트 : "+comment); map.put("comment", comment); return new ModelAndView("single_res", map);
} @RequestMapping("/singleUpDTO.do") public ModelAndView singleUpDTO(SingleUpDTO sto)throws Exception{ MultipartFile upfile = sto.getUpfile(); String comment = sto.getComment(); if(!upfile.isEmpty()){ System.out.println("파일명 : "+upfile.getOriginalFilename()); System.out.println("파일크기 : "+upfile.getSize()); //파일이동 upfile.transferTo(new File(uploadDirectory,upfile.getOriginalFilename())); } System.out.println("comment : "+comment); return new ModelAndView("single_res","sto",sto); } @RequestMapping("/singleUpReq.do") public ModelAndView singleUpReq(MultipartHttpServletRequest request)throws Exception{ System.out.println("---------getFileNames() : 요청파라미터의 이름들 리턴 - type이 file인---------"); Iterator<String> itr = request.getFileNames(); while(itr.hasNext()){ System.out.println(itr.next()); } System.out.println("---------getParameter(), getParameterValues()"); System.out.println("코멘트 : "+request.getParameter("comment")); System.out.println("--------getFile() : 업로드 파일정보 조회"); MultipartFile upfile = request.getFile("upfile"); if(!upfile.isEmpty()){ System.out.println("파일명 : "+upfile.getOriginalFilename()); upfile.transferTo(new File(uploadDirectory, upfile.getOriginalFilename())); } SingleUpDTO sto = new SingleUpDTO(); sto.setComment(request.getParameter("comment")); sto.setUpfile(upfile); return new ModelAndView("single_res","sto2",sto); } }
접기
접기
package springmvc.fileup.dto;
import org.springframework.web.multipart.MultipartFile;
public class SingleUpDTO { private String comment; private MultipartFile upfile;//업로드된 파일 정보를 저장할 변수 public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public MultipartFile getUpfile() { return upfile; } public void setUpfile(MultipartFile upfile) { this.upfile = upfile; } }
접기
접기
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 업로드된 파일<br> Comment : ${requestScope.comment }<br> 파일 : ${requestScope.fileName } <hr> singleUpDTO.do 응답처리<br> Comment : ${requestScope.sto.comment }<br> 파일 : ${requestScope.sto.upfile.originalFilename } <hr> singleUpReq.do 응답처리<br> Comment : ${requestScope.sto2.comment }<br> 파일 : ${requestScope.sto2.upfile.originalFilename } </body> </html>
접기
접기
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans " 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-3.0.xsd "> <!-- ViewResolver --> <bean name="vr" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/res/"/> <property name="suffix" value=".jsp"/> </bean> <!-- multipartResolver --> <bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <!-- <property name="maxUploadSize" value="1048576"/> --><!--1M : 1024*1024, 1048576 --> </bean> <!-- Controller --> <bean name="fileUploadController" class="springmvc.fileup.ctr.FileUploadController"> <constructor-arg value="D:\apache-tomcat-6.0.35\webapps\SpringMVC_04_fileUpload\upload"/> </bean> </beans>
<!-- /application Root/upload -->
접기
접기
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xmlns="http://java.sun.com/xml/ns/javaee " xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " id="WebApp_ID" version="2.5"> <!-- DispatcherServlet 등록(front controller) --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 한글 encoding 처리 --> <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> </web-app>
접기
접기
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 파일 업로드 요청<br> <form action="singleUp.do" method="post" enctype="multipart/form-data"> comment : <input type="text" name="comment"><br> 파일 : <input type="file" name="upfile"><br> <input type="submit" value="전송"> </form> <hr> DTO 이용 <form action="singleUpDTO.do" method="post" enctype="multipart/form-data"> comment : <input type="text" name="comment"><br> 파일 : <input type="file" name="upfile"><br> <input type="submit" value="전송"> </form> <hr> Request 이용<br> <form action="singleUpReq.do" method="post" enctype="multipart/form-data"> comment : <input type="text" name="comment"><br> 파일 : <input type="file" name="upfile"><br> <input type="submit" value="전송"> </form> </body> </html>
접기