티스토리 뷰

Programming/Java

Spring MVC - Form 데이터 전달

파란크리스마스 2017. 3. 26. 19:51
728x90

기본 Form 데이터 전달

기본 Form 데이터 전달 JSP - /sample/form1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<head>
<body>

<form action="<c:url value="/form/form1.do"/>" method="get">
	<input type="text" name="user_id">
	<input type="password" name="password">
	<input type="submit">
</form>

</body>
</html>

기본 Form 데이터 전달 Controller

@Controller
public class FormController {
	
	@RequestMapping(value = "/form/form1.do", method = RequestMethod.GET)
	public String form1(
			@RequestParam("user_id") String user_id, 
			@RequestParam("password") String password,
			ModelMap modelMap) throws Exception 
	{
		System.out.println("user_id = " + user_id + "/" + password);
		return "redirect:/sample/form1.jsp";
	}

파일 업로드 Form 데이터 전달

파일 업로드 Form 데이터 전달 JSP - /sample/form2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<head>
<body>

<form action="<c:url value="/form/form2.do"/>" method="post" enctype="multipart/form-data">
	<input type="text" name="title"><br/>
	<input type="file" name="file1"><br/>
	<input type="file" name="file2"><br/>
	<input type="submit">
</form>

</body>
</html>

파일 업로드 Form 데이터 전달 Controller

@Controller
public class FormController {
	
	@RequestMapping(value = "/form/form2.do", method = RequestMethod.POST)
	public String form2(
			@RequestParam("title") String title, 
			@RequestParam("file1") MultipartFile file1, 
			@RequestParam("file2") MultipartFile file2, 
			ModelMap modelMap) 
	{
		System.out.println("title = " + title);
		
		String uuid_filename1 = fileWrite(file1);
		String uuid_filename2 = fileWrite(file2);
		
		System.out.println(uuid_filename1 + "/" + uuid_filename2);
		
		return "redirect:/sample/form2.jsp";
	}
	
	// 파일 저장 메소드
	private String fileWrite(MultipartFile uploadfile) {
		OutputStream out = null;
		String targetFilename = null;
		try {
			String filePath = "C:/test"; // 설정파일로 뺀다.
			
			// 파일명 얻기
			String fileName = uploadfile.getOriginalFilename();
			
			// 파일의 바이트 정보 얻기
			byte[] bytes = uploadfile.getBytes();
	
			String originalFilename = uploadfile.getOriginalFilename(); // 파일명
			
			// 파일 확장자 추출
			String fileExt = FileUtils.getFileExt(originalFilename);
	
			UUID uuid = UUID.randomUUID();
			targetFilename = uuid.toString() + "." + fileExt.toLowerCase();
	
			String fileFullPath = filePath + "/" + targetFilename; // 파일 전체 경로
			
			// 파일 객체 생성
			File file = new File(fileFullPath);
			// 상위 폴더 존재 여부 확인
			if (!file.getParentFile().exists()) {
				// 상위 폴더가 존재 하지 않는 경우 상위 폴더 생성
				file.getParentFile().mkdirs();
			}
			
			// 파일 아웃풋 스트림 생성
			out = new FileOutputStream(file);
			// 파일 아웃풋 스트림에 파일의 바이트 쓰기
			out.write(bytes);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); }
		}
		return targetFilename;
	}
}
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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
글 보관함