JSP ํ์ผ ์ ๋ก๋
ํ์ผ ์ ๋ก๋
: ํด๋ผ์ด์ธํธ์์ ์ ์กํ ํ์ผ์ ์๋ฒ ์ธก์ ์ ์ฅํ๋๊ฒ
โข
ํ์ผ ๋ฐ์ดํฐ๋ ํ์ผ ์์คํ
์ ์ ์ฅ
โข
ํ์ผ ๊ฒฝ๋ก๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ
JSP ํ์ผ ์ ๋ก๋
์๋ฐ Servlet/JSP ์์๋ ํ์ผ ์
๋ก๋๋ฅผ ๊ตฌํํ๊ธฐ ์ํด ์๋ ์คํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ๋ค.
โข
cos.jar
โข
commons-fileupload.jar
ํ์ผ ์ ๋ก๋ ํผ ํ๊ทธ ์์ฑ
<form action="์์ฒญ๊ฒฝ๋ก" method="post" enctype="multipart/form-data">
<input type="file" name="file" multiple />
</form>
HTML
๋ณต์ฌ
ํ์ผ ์ ์ก์ ํ๊ธฐ ์ํ ํผ ํ๊ทธ๋ฅผ ์์ฑํ ๋๋ enctype ์์ฑ์ โmultipart/form-dataโ ์ผ๋ก ์ง์ ํด์ผํ๋ค.
โข
enctype : ํผ ๋ฐ์ดํฐ๊ฐ ์๋ฒ๋ก ์ ์ก๋ ๋, ๋ฐ์ดํฐ๋ฅผ ์ธ์ฝ๋ฉํ๋ ๋ฐฉ๋ฒ์ ์ง์ ํ๋ ์์ฑ
* method="post" ์ผ ๋๋ง ์ฌ์ฉ๊ฐ๋ฅ
โฆ
application/x-www-form-urlencoded : ์๋ฒ๋ก ๋ณด๋ด๊ธฐ ์ ์ ์ธ์ฝ๋ฉ
โฆ
multipart/form-data : ๋ชจ๋ ๋ฌธ์๋ฅผ ์ธ์ฝ๋ฉํ์ง ์์
โข
input ํ๊ทธ์ type ์์ฑ์ file ๋ก ์ง์ ํ๋ฉด ํ์ผ์ ์ ํํ ์ ์๋ ์
๋ ฅ ํ๊ทธ๊ฐ ๋ง๋ค์ด์ง๋ค.
โข
input type=โfileโ ํ๊ทธ์ multiple ์์ฑ์ ์ถ๊ฐํ๋ฉด, ์ฌ๋ฌ ํ์ผ์ ์ ํํ ์ ์๋ค.
๊ฐ๋ฐํ๊ฒฝ ์ฒดํฌ
Tomcat 8.5
Servlet 3.1
JDK 1.8 ์ด์
์์ ๊ฐ๋ฐํ๊ฒฝ๊ณผ ํธํ๋๋ ํ์ผ ์
๋ก๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
โข
cos.jar
โข
commons-fileupload.jar
cos.jar (MultipartRequest) ๋ฅผ ์ด์ฉํ ํ์ผ ์ ๋ก๋
2.
ํ๋ก์ ํธ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ผ ํฌํจํ๊ธฐ
a.
Project
i.
webapp/WEB-INF/lib
ii.
cos.jar ํ์ผ ์ ์ฅํ๊ธฐ
3.
form ์ ์ก ์ฝ๋ ์์ฑํ๊ธฐ - fileupload01.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>
<!--
enctype : ํผ ๋ฐ์ดํฐ๊ฐ ์๋ฒ๋ก ์ ์ก๋ ๋, ๋ฐ์ดํฐ๋ฅผ ์ธ์ฝ๋ฉํ๋ ๋ฐฉ๋ฒ์ ์ง์ ํ๋ ์์ฑ
* method="post" ์ผ ๋๋ง ์ฌ์ฉ๊ฐ๋ฅ
- application/x-www-form-urlencoded : ์๋ฒ๋ก ๋ณด๋ด๊ธฐ ์ ์ ์ธ์ฝ๋ฉ
- multipart/form-data : ๋ชจ๋ ๋ฌธ์๋ฅผ ์ธ์ฝ๋ฉํ์ง ์์
* ํ์ผ, ์ด๋ฏธ์ง ์ ์กํ ๋ ์ฌ์ฉ
- text/plain : ๊ณต๋ฐฑ๋ง + ๋ก ๋ณํ, ๋๋จธ์ง๋ ์ธ์ฝ๋ฉํ์ง ์์
-->
<!-- jsp ๋ก ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ -->
<form action="fileupload01_pro.jsp" method="post" enctype="multipart/form-data">
<!-- Servlet ์ผ๋ก ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ -->
<%-- <form action="<%= request.getContextPath() %>/fileupload/cos" method="post" enctype="multipart/form-data"> --%>
<p>์ด ๋ฆ : <input type="text" name="name"> </p>
<p>์ ๋ชฉ : <input type="text" name="title"> </p>
<!-- multiple : ์ฌ๋ฌ ํ์ผ์ ์ ํํ๋๋ก ์ง์ -->
<p>ํ ์ผ : <input type="file" name="file1" > </p>
<p>ํ ์ผ : <input type="file" name="file2" > </p>
<p>
<input type="submit" value="์
๋ก๋" />
</p>
</form>
</body>
</html>
HTML
๋ณต์ฌ
4.
ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ ์ฝ๋ ์์ฑํ๊ธฐ(1) - fileupload01_pro.jsp
<%@page import="java.net.URL"%>
<%@page import="java.io.File"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์์ฒญํ ํ์ผ์ ๋ค๋ฃจ๋ ํด๋์ค
// MultipartRequest(request, ์ ์ฅ๊ฒฝ๋ก, ํ์ผ ์ต๋ํฌ๊ธฐ, ์ธ์ฝ๋ฉํ์
, ํ์ผ๋ช
์ ์ฑ
๊ฐ์ฒด)
String uploadPath = "C:/upload/";
out.println("upload ๊ฒฝ๋ก : " + uploadPath);
int maxSize = 10 * 1024 * 1024; // 10MB
DefaultFileRenamePolicy policy = new DefaultFileRenamePolicy();
MultipartRequest multi = new MultipartRequest(request, uploadPath, maxSize, "UTF-8", policy);
// enctype="multipart/form-data" ์ผ๋ก ์์ฒญํ๋ ๊ฒฝ์ฐ
// MultipartRequest ๋ก ์์ฒญ ํ๋ผ๋ฏธํฐ ํ์ธ
Enumeration params = multi.getParameterNames();
while( params.hasMoreElements() ) {
String name = (String) params.nextElement();
String value = multi.getParameter(name);
out.println(name + " : " + value + "<br>");
}
out.println("----------------------------------------------<br>");
// ํ์ผ
Enumeration files = multi.getFileNames();
while( files.hasMoreElements() ) {
String file = (String) files.nextElement();
String fileName = multi.getFilesystemName(file); // ์๋ฒ์ ์
๋ก๋๋ ํ์ผ๋ช
String original = multi.getOriginalFileName(file); // ํด๋ผ์ด์ธํธ์์ ์
๋ก๋ํ ํ์ผ๋ช
String type = multi.getContentType(file); // ์ปจํ
์ธ ํ์
File f = multi.getFile(file);
out.println("์์ฒญ ํ๋ผ๋ฏธํฐ ์ด๋ฆ : " + file + "<br>");
out.println("์ค์ ํ์ผ ์ด๋ฆ : " + original + "<br>");
out.println("์ ์ฅ ํ์ผ ์ด๋ฆ : " + fileName + "<br>");
out.println("ํ์ผ ์ฝํ
์ธ ํ์
: " + type + "<br>");
out.println("์ ์ฅ ๊ฒฝ๋ก : " + f.getPath() + "<br>");
if( f != null ) {
out.println("ํ์ผ ํฌ๊ธฐ : " + f.length() );
out.println("<br>");
}
out.println("----------------------------------------------<br>");
}
%>
Java
๋ณต์ฌ
5.
ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ ์ฝ๋ ์์ฑํ๊ธฐ(2) - FileUploadServlet.java
a.
์๋ธ๋ฆฟ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ
package fileupload.cos;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
@WebServlet("/fileupload/cos")
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
// ํ๋ก์ ํธ ๊ฒฝ๋ก(root) ๊ฐ์ ธ์ค๊ธฐ
String className = getClass().getName().substring( getClass().getName().lastIndexOf(".") + 1 );
URL resourceUrl = getClass().getResource(className+".class");
System.out.println("class : " + className);
String root = "";
if (resourceUrl != null) {
String path = resourceUrl.getFile();
int lastIndex = path.lastIndexOf("WEB-INF");
root = path.substring(0, lastIndex);
System.out.println("ํ์ผ ์์น: " + resourceUrl.getFile());
System.out.println("ํ๋ก์ ํธ ์์น: " + root);
} else {
System.err.println("๋ฆฌ์์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.");
}
// ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์์ฒญํ ํ์ผ์ ๋ค๋ฃจ๋ ํด๋์ค
// MultipartRequest(request, ์ ์ฅ๊ฒฝ๋ก, ํ์ผ ์ต๋ํฌ๊ธฐ, ์ธ์ฝ๋ฉํ์
, ํ์ผ๋ช
์ ์ฑ
๊ฐ์ฒด)
root = "C:/"; // ๊ณ ์ ๋ ๊ฒฝ๋ก๋ก ํ๋ ๊ฒฝ์ฐ
String filePath = root + "upload/";
System.out.println("upload ๊ฒฝ๋ก : " + filePath);
int maxSize = 10 * 1024 * 1024; // 10MB
DefaultFileRenamePolicy policy = new DefaultFileRenamePolicy();
MultipartRequest multi = new MultipartRequest(request, filePath, maxSize, "UTF-8", policy);
// enctype="multipart/form-data" ์ผ๋ก ์์ฒญํ๋ ๊ฒฝ์ฐ
// MultipartRequest ๋ก ์์ฒญ ํ๋ผ๋ฏธํฐ ํ์ธ
Enumeration params = multi.getParameterNames();
while( params.hasMoreElements() ) {
String name = (String) params.nextElement();
String value = multi.getParameter(name);
out.println(name + " : " + value + "<br>");
}
out.println("----------------------------------------------<br>");
// ํ์ผ
Enumeration files = multi.getFileNames();
while( files.hasMoreElements() ) {
String file = (String) files.nextElement();
String fileName = multi.getFilesystemName(file); // ์๋ฒ์ ์
๋ก๋๋ ํ์ผ๋ช
String original = multi.getOriginalFileName(file); // ํด๋ผ์ด์ธํธ์์ ์
๋ก๋ํ ํ์ผ๋ช
String type = multi.getContentType(file); // ์ปจํ
์ธ ํ์
File f = multi.getFile(file);
out.println("์์ฒญ ํ๋ผ๋ฏธํฐ ์ด๋ฆ : " + file + "<br>");
out.println("์ค์ ํ์ผ ์ด๋ฆ : " + original + "<br>");
out.println("์ ์ฅ ํ์ผ ์ด๋ฆ : " + fileName + "<br>");
out.println("ํ์ผ ์ฝํ
์ธ ํ์
: " + type + "<br>");
out.println("์ ์ฅ ๊ฒฝ๋ก : " + f.getPath() + "<br>");
if( f != null ) {
out.println("ํ์ผ ํฌ๊ธฐ : " + f.length() );
out.println("<br>");
}
out.println("----------------------------------------------<br>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Java
๋ณต์ฌ
๊ฒฐ๊ณผํ๋ฉด
commons-fileupload.jar (DiskFileUpload) ๋ฅผ ์ด์ฉํ ํ์ผ ์ ๋ก๋
3.
ํ๋ก์ ํธ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ผ ํฌํจํ๊ธฐ
a.
Project
b.
ํฌํจํ๊ธฐ
i.
webapp/WEB-INF/lib
ii.
commons-io.jar ํ์ผ ์ ์ฅํ๊ธฐ
iii.
commons-fileupload.jar ํ์ผ ์ ์ฅํ๊ธฐ
4.
form ์ ์ก ์ฝ๋ ์์ฑํ๊ธฐ - fileupload02.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํ์ผ ์
๋ก๋ - common-fileupload</title>
</head>
<body>
<form action="fileupload02_pro.jsp" method="post" enctype="multipart/form-data">
<p>ํ์ผ : <input type="file" name="file" multiple /></p>
<p><input type="submit" value="์
๋ก๋" /></p>
</form>
</body>
</html>
HTML
๋ณต์ฌ
5.
ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ ์ฝ๋ ์์ฑํ๊ธฐ - fileupload02_pro.jsp
<%@page import="java.util.UUID"%>
<%@page import="java.io.File"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String fileUploadPath = "C:/upload";
// commons-fileupload ์์ ํ์ผ ์
๋ก๋ ๊ธฐ๋ฅ์ ๊ฐ์ง ๊ฐ์ฒด DiskFileUpload ์์ฑ
DiskFileUpload upload = new DiskFileUpload();
// parseRequest(request)
// : request ๊ฐ์ฒด๋ฅผ ๋ถ์ํ์ฌ, multipart/form-data ์ ํ์ ํ๋ผ๋ฏธํฐ๋ง ์ฝ์ด๋๋ ค์ List๋ก ๋ฐํ
List<FileItem> items = upload.parseRequest(request); // FileItem ํ์
์ ํ๋ผ๋ฏธํฐ ๋ชฉ๋ก
// for( int i = 0 ; i < items.size() ; i++ ) {
// FileItem item = (FileItem) items.get(i);
// }
Iterator<FileItem> params = items.iterator(); // ๋ฐ๋ณต ๊ฐ์ฒด
while( params.hasNext() ) {
FileItem fileItem = params.next(); // ํ์ผ ์์
// isFormField() : ์์ฒญ ํ๋ผ๋ฏธํฐ๊ฐ ์ผ๋ฐ ๋ฐ์ดํฐ ์ธ์ง ํ์ธ (์ผ๋ฐ:true, ํ์ผ:false)
// ํ์ผ์ธ์ง ํ์ธ
if( !fileItem.isFormField() ) {
// ํ์ผ๋ช
์ค๋ณต์ ๋ฐฉ์งํ๊ธฐ ์ํด
// UID_ํ์ผ๋ช
.ํ์ฅ์ ํ์์ผ๋ก ํ์ผ๋ช
์ง์ ํ๊ธฐ : UID_๊ฐ์์ง.jpg
String fileName = UUID.randomUUID() + "_" + fileItem.getName();
File file = new File(fileUploadPath + "/" + fileName); // ~/UPLOAD/๊ฐ์์ง.jpg ๊ฒฝ๋ก๋ก ํ์ผ ๊ฐ์ฒด ์์ฑ
fileItem.write(file); // write() : ํ์ผ ์ ์ฅ
}
}
out.print("<h1>ํ์ผ ์
๋ก๋ ์ฑ๊ณต!</h1>");
%>
Java
๋ณต์ฌ
๊ฒฐ๊ณผํ๋ฉด
commons-fileupload.jar ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ผ๋ฐ ๋ฐ์ดํฐ, ํ์ผ ๋ฐ์ดํฐ ์ฒ๋ฆฌํ๊ธฐ
fileupload03.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํ์ผ ์
๋ก๋ - common-fileupload</title>
</head>
<body>
<!-- jsp ๋ก ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ -->
<!-- <form action="fileupload02_pro.jsp" method="post" enctype="multipart/form-data"> -->
<!-- Servlet ์ผ๋ก ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ -->
<form action="<%= request.getContextPath() %>/fileupload/commons" method="post" enctype="multipart/form-data">
<p>์ด๋ฆ : <input type="text" name="name" /></p>
<p>์ ๋ชฉ : <input type="text" name="title" /></p>
<p>ํ์ผ : <input type="file" name="file" multiple /></p>
<p><input type="submit" value="์
๋ก๋" /></p>
</form>
</body>
</html>
HTML
๋ณต์ฌ
fileupload03_pro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ํ์ผ ์
๋ก๋ - common-fileupload</title>
</head>
<body>
<!-- jsp ๋ก ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ -->
<form action="fileupload03_pro.jsp" method="post" enctype="multipart/form-data">
<!-- Servlet ์ผ๋ก ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ -->
<%-- <form action="<%= request.getContextPath() %>/fileupload/commons" method="post" enctype="multipart/form-data"> --%>
<p>์ด๋ฆ : <input type="text" name="name" /></p>
<p>์ ๋ชฉ : <input type="text" name="title" /></p>
<p>ํ์ผ : <input type="file" name="file" multiple /></p>
<p><input type="submit" value="์
๋ก๋" /></p>
</form>
</body>
</html>
Java
๋ณต์ฌ
FileUploadServlet.java
package fileupload.commons;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
@WebServlet(name = "CommonsFileUploadServlet", urlPatterns = "/fileupload/commons")
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
String fileUploadPath = "C:/UPLOAD";
// commons-fileupload ์์ ํ์ผ ์
๋ก๋ ๊ธฐ๋ฅ์ ๊ฐ์ง ๊ฐ์ฒด DiskFileUpload ์์ฑ
DiskFileUpload upload = new DiskFileUpload();
// parseRequest(request)
// : request ๊ฐ์ฒด๋ฅผ ๋ถ์ํ์ฌ, multipart/form-data ์ ํ์ ํ๋ผ๋ฏธํฐ๋ง ์ฝ์ด๋๋ ค์ List๋ก ๋ฐํ
List<FileItem> items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // FileItem ํ์
์ ํ๋ผ๋ฏธํฐ ๋ชฉ๋ก
if( items == null ) {
out.print("<h1>ํ์ผ ์
๋ก๋ ์คํจ!</h1>");
}
Iterator<FileItem> params = items.iterator(); // ๋ฐ๋ณต ๊ฐ์ฒด
while( params.hasNext() ) {
FileItem fileItem = params.next(); // ํ์ผ ์์
// isFormField() : ์์ฒญ ํ๋ผ๋ฏธํฐ๊ฐ ์ผ๋ฐ ๋ฐ์ดํฐ ์ธ์ง ํ์ธ (์ผ๋ฐ:true, ํ์ผ:false)
// ํ์ผ์ธ์ง ํ์ธ
if( !fileItem.isFormField() ) {
// ํ์ผ๋ช
์ค๋ณต์ ๋ฐฉ์งํ๊ธฐ ์ํด
// UID_ํ์ผ๋ช
.ํ์ฅ์ ํ์์ผ๋ก ํ์ผ๋ช
์ง์ ํ๊ธฐ : UID_๊ฐ์์ง.jpg
String fileName = UUID.randomUUID() + "_" + fileItem.getName();
File file = new File(fileUploadPath + "/" + fileName); // ~/UPLOAD/๊ฐ์์ง.jpg ๊ฒฝ๋ก๋ก ํ์ผ ๊ฐ์ฒด ์์ฑ
try {
fileItem.write(file); // write() : ํ์ผ ์ ์ฅ
} catch (Exception e) {
e.printStackTrace();
}
}
}
out.print("<h1>ํ์ผ ์
๋ก๋ ์ฑ๊ณต!</h1>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Java
๋ณต์ฌ