[Servlet 3-2] File View

μž„μŠΉν˜„Β·2022λ…„ 11μ›” 30일
0

Servlet

λͺ©λ‘ 보기
7/14

πŸŽ¨μ˜¬λ¦°μ΄μ™€ νŒŒμΌμ„ μž…λ ₯λ°›μ•„ μ²˜λ¦¬νŽ˜μ΄μ§€(view.itwill)λ₯Ό μš”μ²­ν•˜μ—¬ μ „λ‹¬ν•˜λŠ” μ›Ήλ¬Έμ„œ

πŸ“’ μ‚¬μš©μžλ‘œλΆ€ν„° νŒŒμΌμ„ μž…λ ₯λ°›μ•„ μ›Ήν”„λ‘œκ·Έλž¨μ„ μš”μ²­ν•˜μ—¬ μ „λ‹¬ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ°˜λ“œμ‹œ method 속성값을 [POST]둜 μ„€μ •ν•˜κ³  enctype 속성값을 [multipart/form-data]둜 μ„€μ •
πŸ“Œ enctype 속성 : [application/x-www-form-urlencoded] λ˜λŠ” [multipart/form-data] 쀑 ν•˜λ‚˜λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
β†’ [application/x-www-form-urlencoded] : μž…λ ₯값을 인코딩 처리된 λ¬Έμžκ°’μœΌλ‘œ 전달(κΈ°λ³Έ) - 파일 전달 λΆˆκ°€λŠ₯
β†’ [multipart/form-data] : λͺ¨λ“  μž…λ ₯값을 μ›μ‹œλ°μ΄νƒ€(λ©€ν‹°νŒŒνŠΈ 폼데이타)둜 전달 - 파일 전달 κ°€λŠ₯

πŸ“ƒfile_view.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- μ˜¬λ¦°μ΄μ™€ νŒŒμΌμ„ μž…λ ₯λ°›μ•„ μ²˜λ¦¬νŽ˜μ΄μ§€(view.itwill)λ₯Ό μš”μ²­ν•˜μ—¬ μ „λ‹¬ν•˜λŠ” μ›Ήλ¬Έμ„œ -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Servlet</title>
<link rel="stylesheet" href="css/common.css" type="text/css"/>
</head>
<body>
<h1>파일 μ—…λ‘œλ“œ-1</h1>
<hr/>
μž…λ ₯ 데이타 및 μž…λ ₯ νŒŒμΌμ„ μ›ΉλΈŒλΌμš°μ €μ— 좜λ ₯<br/>
<hr/>
<!-- μ‚¬μš©μžλ‘œλΆ€ν„° νŒŒμΌμ„ μž…λ ₯λ°›μ•„ μ›Ήν”„λ‘œκ·Έλž¨μ„ μš”μ²­ν•˜μ—¬ μ „λ‹¬ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ°˜λ“œμ‹œ method 
 속성값을 [POST]둜 μ„€μ •ν•˜κ³  enctype 속성값을 [multipart/form-data]둜 μ„€μ • -->
<!-- enctype 속성 : [application/x-www-form-urlencoded] λ˜λŠ” [multipart/form-data] 쀑 ν•˜λ‚˜λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ • -->
<!-- => [application/x-www-form-urlencoded] : μž…λ ₯값을 인코딩 처리된 λ¬Έμžκ°’μœΌλ‘œ 전달(κΈ°λ³Έ) - 파일 전달 λΆˆκ°€λŠ₯ -->
<!-- => [multipart/form-data] : λͺ¨λ“  μž…λ ₯값을 μ›μ‹œλ°μ΄νƒ€(λ©€ν‹°νŒŒνŠΈ 폼데이타)둜 전달 - 파일 전달 κ°€λŠ₯ -->
<form action="view.itwill" method="post" enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="0" width="500" align="center">
<tr>
	<td valign="top" bgcolor="#000000" align="center">
		<table border="0" cellpadding="1" cellspacing="1" width="500">
			<tr bgcolor="#556b2f" class="menu">
				<td align="center" colspan="2">
					λ©€ν‹°νŒŒνŠΈ/νŒŒμΌμ—…λ‘œλ“œ(μ—…λ‘œλ“œ 데이터 보기)
				</td>
			</tr>
			<tr bgcolor="#f4f4f4">
				<td width="100" align="center" class="t1">
					올린이
				</td>
				<td width="400" align="left">
					&nbsp;<input type="text" name="name" 
						size="10" maxlength="10" class="TXTFLD">
				</td>
			</tr>
			<tr bgcolor="#f4f4f4">
				<td width="100" align="center" class="t1">
					νŒŒμΌμ„ νƒ
				</td>
				<td width="400" align="left">
					&nbsp;<input type="file" name="fileone" 
						size="40" class="TXTFLD">
				</td>
			</tr>
			<tr bgcolor="#f4f4f4">
				<td align="center" colspan="2">
					<input type="submit" value="μ˜¬λΌκ°€λΌ..">
				</td>
			</tr>
		</table>
	</td>
</tr>
</table>
</form>
</body>
</html>



πŸŽ¨μž…λ ₯νŽ˜μ΄μ§€(file_view.html)μ—μ„œ μ „λ‹¬λœ μž…λ ₯κ°’(올린이)κ³Ό μž…λ ₯νŒŒμΌμ„ λ°˜ν™˜λ°›μ•„ ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ μ „λ‹¬ν•˜λŠ” μ„œλΈ”λ ›

β†’ μž…λ ₯νŽ˜μ΄μ§€μ—μ„œ μ „λ‹¬λœ μ›μ‹œλ°μ΄ν„°(λ©€ν‹°νŒŒνŠΈ 폼데이터)λ₯Ό ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ μ „λ‹¬ν•˜μ—¬ 응닡 처리

πŸ“’ 비정상적인 μš”μ²­μ— λŒ€ν•œ 처리

if(request.getMethod().equals("GET")) {
	response.sendRedirect("file_view.html");
	return;
	}

/-------------------------------------------------------------------
πŸ“’ λ¦¬ν€˜μŠ€νŠΈ λ©”μ„Έμ§€μ˜ λͺΈμ²΄λΆ€μ˜ μ €μž₯값에 λŒ€ν•œ 캐릭터셋 λ³€κ²½

request.setCharacterEncoding("utf-8");

πŸ“’ 전달값(μ „λ‹¬νŒŒμΌ)을 λ°˜ν™˜λ°›μ•„ μ €μž₯
β†’ μ›μ‹œλ°μ΄ν„°(λ©€ν‹°νŒŒνŠΈ 폼데이터)둜 μ „λ‹¬λ˜λŠ” κ°’ λ˜λŠ” νŒŒμΌμ€ HttpServletRequest 객체의 getParameter() λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ κ°’ λ˜λŠ” 파일의 λ°˜ν™˜ λΆˆκ°€λŠ₯

String name=request.getParameter("name");
String fileone=request.getParameter("fileone");

πŸ“’ 응닡결과 생성

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset='UTF-8'>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>μž…λ ₯κ°’κ³Ό μž…λ ₯파일</h1>");
out.println("<hr>");
out.println("<p>올린이 = "+name+"</p>");
out.println("<p>파일λͺ… = "+fileone+"</p>");
out.println("</body>");
out.println("</html>")

-------------------------------------------------------------------/

πŸ“Œ HttpServletRequest.getInputStream() : λ¦¬ν€˜μŠ€νŠΈ λ©”μ„Έμ§€μ˜ λͺΈμ²΄λΆ€μ— μ €μž₯λ˜μ–΄ μ „λ‹¬λ˜λŠ” μ›μ‹œλ°μ΄ν„°λ₯Ό 읽기 μœ„ν•œ μž…λ ₯슀트림(ServletInputStream 객체)을 λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œ

ServletInputStream in=request.getInputStream();

πŸ“ƒFileViewServlet.java

package xyz.itwill.servlet;
//
import java.io.IOException;
import java.io.PrintWriter;
//
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//
//μž…λ ₯νŽ˜μ΄μ§€(file_view.html)μ—μ„œ μ „λ‹¬λœ μž…λ ₯κ°’(올린이)κ³Ό μž…λ ₯νŒŒμΌμ„ λ°˜ν™˜λ°›μ•„ ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ μ „λ‹¬ν•˜λŠ” μ„œλΈ”λ ›
//β†’ μž…λ ₯νŽ˜μ΄μ§€μ—μ„œ μ „λ‹¬λœ μ›μ‹œλ°μ΄ν„°(λ©€ν‹°νŒŒνŠΈ 폼데이터)λ₯Ό ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ μ „λ‹¬ν•˜μ—¬ 응닡 처리
@WebServlet("/view.itwill")
public class FileViewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		//
		//비정상적인 μš”μ²­μ— λŒ€ν•œ 처리
		if(request.getMethod().equals("GET")) {
			response.sendRedirect("file_view.html");
			return;
		}
		//
		/*
		//λ¦¬ν€˜μŠ€νŠΈ λ©”μ„Έμ§€μ˜ λͺΈμ²΄λΆ€μ˜ μ €μž₯값에 λŒ€ν•œ 캐릭터셋 λ³€κ²½
		request.setCharacterEncoding("utf-8");
		//
		//전달값(μ „λ‹¬νŒŒμΌ)을 λ°˜ν™˜λ°›μ•„ μ €μž₯
		//β†’ μ›μ‹œλ°μ΄ν„°(λ©€ν‹°νŒŒνŠΈ 폼데이터)둜 μ „λ‹¬λ˜λŠ” κ°’ λ˜λŠ” νŒŒμΌμ€ HttpServletRequest 객체의 getParameter() λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ κ°’ λ˜λŠ” 파일의 λ°˜ν™˜ λΆˆκ°€λŠ₯
		String name=request.getParameter("name");
		String fileone=request.getParameter("fileone");
		//
		//응닡결과 생성
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta charset='UTF-8'>");
		out.println("<title>Servlet</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>μž…λ ₯κ°’κ³Ό μž…λ ₯파일</h1>");
		out.println("<hr>");
		out.println("<p>올린이 = "+name+"</p>");
		out.println("<p>파일λͺ… = "+fileone+"</p>");
		out.println("</body>");
		out.println("</html>");
		*/
		//
		//HttpServletRequest.getInputStream() : λ¦¬ν€˜μŠ€νŠΈ λ©”μ„Έμ§€μ˜ λͺΈμ²΄λΆ€μ— μ €μž₯λ˜μ–΄ μ „λ‹¬λ˜λŠ” μ›μ‹œλ°μ΄ν„°λ₯Ό 읽기 μœ„ν•œ μž…λ ₯슀트림(ServletInputStream 객체)을 λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œ
		ServletInputStream in=request.getInputStream();
		//
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta charset='UTF-8'>");
		out.println("<title>Servlet</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>μž…λ ₯κ°’κ³Ό μž…λ ₯파일</h1>");
		out.println("<hr>");
		out.println("<p>λ¦¬ν€˜μŠ€νŠΈ λ©”μ„Έμ§€μ˜ λͺΈμ²΄λΆ€μ— μ €μž₯λ˜μ–΄ μ „λ‹¬λœ μ›μ‹œλ°μ΄ν„°(λ©€ν‹°νŒŒνŠΈ 폼데이터)</p>");
		out.println("<hr>");
		out.println("<pre>");
		while(true) {
			int readByte=in.read();
			if(readByte==-1) break;
			out.write(readByte);
		}
		out.println("</pre>");
		out.println("</body>");
		out.println("</html>");
	}
}

0개의 λŒ“κΈ€