๊ฐ๋ฐํ๊ฒฝ ์ฒดํฌ
ํ ์คํธ
๋ผ์ด๋ธ๋ฌ๋ฆฌ
โข
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>
<!-- ์๋ธ๋ฆฟ์ผ๋ก ์์ฒญ -->
<%-- <form action="<%= request.getContextPath() %>/upload" method="post" enctype="multipart/form-data"> --%>
<!-- JSP๋ก ์์ฒญ -->
<form action="<%= request.getContextPath() %>/fileupload01_pro.jsp" method="post" enctype="multipart/form-data">
<div>
์ด ๋ฆ : <input type="text" name="name" />
</div>
<div>
์ ๋ชฉ : <input type="text" name="title" />
</div>
<div>
ํ ์ผ : <input type="file" name="file1" /> <br>
ํ ์ผ : <input type="file" name="file2" /> <br>
</div>
<div>
<input type="submit" value="์
๋ก๋" />
</div>
</form>
</body>
</html>
HTML
๋ณต์ฌ
โข
fileupload01_pro.jsp
<%@page import="org.apache.commons.fileupload2.jakarta.*"%> <%-- Jakarta Servlet์ ์ํ Commons FileUpload ํด๋์ค ์ํฌํธ --%>
<%@page import="org.apache.commons.fileupload2.core.*"%> <%-- Commons FileUpload์ ์ฝ์ด ๊ธฐ๋ฅ ํด๋์ค ์ํฌํธ --%>
<%@page import="java.nio.charset.StandardCharsets"%> <%-- ๋ฌธ์ ์ธ์ฝ๋ฉ ์์ ์ฌ์ฉ์ ์ํ ์ํฌํธ --%>
<%@page import="java.io.FileOutputStream"%> <%-- ํ์ผ ์ ์ฅ์ ์ํ ์ถ๋ ฅ ์คํธ๋ฆผ ํด๋์ค --%>
<%@page import="java.io.InputStream"%> <%-- ํ์ผ ์
๋ ฅ์ ์ํ ์
๋ ฅ ์คํธ๋ฆผ ํด๋์ค --%>
<%@page import="java.nio.charset.Charset"%> <%-- ๋ฌธ์์
๊ด๋ จ ํด๋์ค --%>
<%@page import="java.io.File"%> <%-- ํ์ผ ๋ฐ ๋๋ ํฐ๋ฆฌ ์ ์ด์ฉ ํด๋์ค --%>
<%@page import="java.util.List"%> <%-- ๋ฆฌ์คํธ ์๋ฃํ ์ฌ์ฉ์ ์ํ ์ํฌํธ --%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%-- JSP ํ์ด์ง ์ค์ (์ธ์ด, ์ฝํ
์ธ ํ์
, ์ธ์ฝ๋ฉ) --%>
<%
// ํ์ผ ์
๋ก๋๊ฐ ์ ์ฅ๋ ๊ฒฝ๋ก ์ค์
String uploadPath = "C:/upload/";
File uploadDir = new File(uploadPath); // ํด๋น ๊ฒฝ๋ก์ ๋ํ File ๊ฐ์ฒด ์์ฑ
if( !uploadDir.exists() ) { // ํด๋น ๊ฒฝ๋ก๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด
uploadDir.mkdirs(); // ๋๋ ํ ๋ฆฌ ์์ฑ
}
// ํด๋ผ์ด์ธํธ ์์ฒญ์ ๋ฌธ์ ์ธ์ฝ๋ฉ ์ค์
request.setCharacterEncoding("UTF-8");
// ์์ ํ์ผ ์ ์ฅ ๊ฒฝ๋ก ์ค์ (java์ ๊ธฐ๋ณธ tmp ๋๋ ํ ๋ฆฌ)
File repository = new File( System.getProperty("java.io.tmpdir") );
// FileItemFactory ์ค์ : ์
๋ก๋๋ ํญ๋ชฉ์ ์์ฑํ๋ ๊ณต์ฅ ๊ฐ์ฒด
FileItemFactory factory = DiskFileItemFactory.builder().setFile(repository).get();
// Servlet ๊ธฐ๋ฐ ํ์ผ ์
๋ก๋ ๊ฐ์ฒด ์์ฑ
JakartaServletFileUpload upload = new JakartaServletFileUpload(factory);
// ํ์ฌ ์์ฒญ์ ํ์ฑํ๊ธฐ ์ํ Context ๊ฐ์ฒด ์์ฑ
JakartaServletRequestContext context = new JakartaServletRequestContext(request);
try {
// ์์ฒญ์์ form field ๋ฐ ํ์ผ์ ํ์ฑํ์ฌ ๋ฆฌ์คํธ๋ก ๊ฐ์ ธ์ด
List<FileItem> items = upload.parseRequest(context);
for( FileItem item : items ) { // ๋ฆฌ์คํธ์ ๊ฐ ํญ๋ชฉ์ ๋ํด ์ฒ๋ฆฌ
// ํ
์คํธ ํผ ํ๋์ธ ๊ฒฝ์ฐ
if( item.isFormField() ) {
// UTF-8๋ก ์ธ์ฝ๋ฉ๋ ๊ฐ ์ถ๋ ฅ
out.println(item.getFieldName() + " : " + item.getString(StandardCharsets.UTF_8) + "<br>");
}
// ํ์ผ ์
๋ก๋ ํ๋์ธ ๊ฒฝ์ฐ
else {
String fieldName = item.getFieldName(); // ํ๋ ์ด๋ฆ
String fileName = item.getName(); // ์
๋ก๋๋ ํ์ผ ์ด๋ฆ
String uploadedName = System.currentTimeMillis() + "_" + item.getName(); // ์ ์ฅ ์ ์ฌ์ฉํ ํ์ผ ์ด๋ฆ
// ํ์ผ๋ช
์ด ๋น์ด์์ง ์์ผ๋ฉด
if( fileName != null && !fileName.isEmpty() ) {
// ์
๋ก๋ ๊ฒฝ๋ก์ ์ ์ฅํ ํ์ผ ๊ฐ์ฒด ์์ฑ
File uploadedFile = new File(uploadDir, fileName);
try (
InputStream is = item.getInputStream(); // ์
๋ก๋๋ ํ์ผ์ ์
๋ ฅ ์คํธ๋ฆผ
FileOutputStream fos = new FileOutputStream(uploadedFile); // ํ์ผ๋ก ์ถ๋ ฅํ๊ธฐ ์ํ ์คํธ๋ฆผ
) {
is.transferTo(fos); // ํ์ผ ์ ์ฅ ์ฒ๋ฆฌ
// ์
๋ก๋ ๊ฒฐ๊ณผ ์ถ๋ ฅ
out.println("============================================ <br>");
out.println("ํ๋ ์ด๋ฆ : " + fieldName + "<br>");
out.println("์๋ณธ ํ์ผ๋ช
: " + fileName + "<br>");
out.println("์ ์ฅ ํ์ผ๋ช
: " + uploadedName + "<br>");
out.println("ํ์
: " + item.getContentType() + "<br>");
out.println("๊ฒฝ๋ก : " + uploadedFile.getAbsolutePath() + "<br>");
out.println("ํฌ๊ธฐ : " + uploadedFile.length() + "<br>");
} catch( Exception e ) {
// ํ์ผ ์ ์ฅ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ ์ถ๋ ฅ
e.printStackTrace();
}
}
}
}
} catch( Exception e ) {
// ์ ์ฒด ์
๋ก๋ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์ ์ ์ถ๋ ฅ
out.println("ํ์ผ ์
๋ก๋ ์ค ์๋ฌ ๋ฐ์");
e.printStackTrace();
}
%>
HTML
๋ณต์ฌ