Search

JSP ํŒŒ์ผ ์—…๋กœ๋“œ

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) ๋ฅผ ์ด์šฉํ•œ ํŒŒ์ผ ์—…๋กœ๋“œ

1.
cos.jar ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด๋กœ๋“œ
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) ๋ฅผ ์ด์šฉํ•œ ํŒŒ์ผ ์—…๋กœ๋“œ

1.
commons-io.jar ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด๋กœ๋“œ
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
๋ณต์‚ฌ

๊ฒฐ๊ณผํ™”๋ฉด