ํ์ผ ์ ๋ก๋ ํผ ๋ง๋ค๊ธฐ
๋ค์์ ๋ณผ HTML ์ฝ๋๊ฐ ๋ฐ๋ก ํ์ผ ์ ๋ก๋ ํผ์ ๋๋ค. ์ฌ๊ธฐ์ ์ค์ํ ์ ์ ๋ช๊ฐ์ง ์ง๊ณ ๋์ด๊ฐ๊ฒ ์ต๋๋ค.
<form>์ method ์์ฑ์ POST ์ ๋๋ค.
<form>์ enctype ์์ฑ์ multipart/form-data ์ ๋๋ค.
<form>์ action ์์ฑ์ ๋ฐฑ์๋ ์ชฝ์์ ํ์ผ ์ ๋ก๋๋ฅผ ํธ๋ค๋งํ JSP ํ์ผ์ ๋๋ค. ์๋ ์์ ๋ UploadFile.jsp๋ฅผ ์ฌ์ฉํ์ต๋๋ค.
ํ๊ฐ์ ํ์ผ๋ง ์ ๋ก๋ ํ ๋๋ <input .../> ํ๊ทธ๋ฅผ type="file" ์์ฑ๊ณ ํจ๊ป ์ฌ์ฉํฉ๋๋ค. ๋ฉํฐํ ํ์ผ์ ๋ก๋ฉ์ ๊ตฌํํ๋ ค๋ฉด ํ๋ ์ด์์ inputํ๊ทธ๋ฅผ ๊ฐ๊ธฐ ๋ค๋ฅธ name์ผ๋ก ์ค์ ํด์ค์ผ ํฉ๋๋ค.
<html> <head> <title>File Uploading Form</title> </head> <body> <h3>File Upload:</h3> Select a file to upload: <br /> <form action="UploadServlet" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="50" /> <br /> <input type="submit" value="Upload File" /> </form> </body> </html>
์ ์์ค๋ฅผ UploadFile.htm์ ๋ฃ์ด์ฃผ์ธ์. ์ ํ์ด์ง๋ฅผ ์ถ๋ ฅํด๋ณด๋ฉด ์๋์ฒ๋ผ ๋์ฌ๊ฑฐ์์. ํ์ผ์ ์ ๋ก๋ ํด๋ณด์ธ์ ^___^ ์ฐธ๊ณ ๋ก ์ ์ฝ๋๋ ๊ทธ๋ฅ dummy์ฝ๋๋ผ์ ์ค์ ๋ก ํ์ผ์ ์ด๋๊ฐ๋ก ์ ๋ก๋ ํ์ง๋ ์์ต๋๋ค.
๋ฐฑ์๋ JSP ์คํฌ๋ฆฝํธ ๋ง๋ค๊ธฐ
์ฐ์ ํ์ผ๋ค์ด ์ ๋ก๋๋์์ ๋ ์ด๋์ ์ ์ฅ๋ ์ง ๊ฒฝ๋ก๋ฅผ ์ง์ ํด๋ณผ๊ฒ์. ๊ฒฝ๋ก๋ฅผ ์ง์ ํ๋ ๋ฐฉ๋ฒ์ ์์ค์ ํ๋์ฝ๋ฉํ๋ ๋ฐฉ๋ฒ๋ ์๊ณ web.xmlํ์ผ์ context-param ํ๊ทธ๋ด์ ์ง์ ํด์ฃผ๋ ๋ฐฉ๋ฒ๋ ์์ต๋๋ค.
<web-app> .... <context-param> <description>Location to store uploaded file</description> <param-name>file-upload</param-name> <param-value> c:\apache-tomcat-5.5.29\webapps\data\ </param-value> </context-param> .... </web-app>
์๋๋ ํ๋ฒ์ ์ฌ๋ฌ๊ฐ์ ํ์ผ์ ์ ๋ก๋ ํ ์ ์๋ UploadFile.jsp์ ์์ค์ฝ๋์์. ์์ค๋ถํฐ ๋ณด๊ธฐ์ ์ ์ ๊ฒํด์ผํ ๊ฒ๋ค์ด ์์ต๋๋ค.
์๋ ์์ ๋ FileUpload ํด๋์ค๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์ต์ ๋ฒ์ ์ commons-fileupload.x.x.jar ํ์ผ์ ํด๋์คํจ์ค์ ๋ฃ์ด์ฃผ์ ์ผ ํฉ๋๋ค. ์ฌ๊ธฐ์ http://commons.apache.org/fileupload/ ๋ค์ด๋ก๋ ๋ฐ์ผ์ค ์ ์์ด์.
๋ํ Commons IO ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ํ์ํฉ๋๋ค. commons-io-x.x.jar ํ์ผ ์ญ์ ํด๋์ค ํจ์ค์ ์ค์ ์ ํด์ฃผ์ ์ผ ๋์. ๋ค์ด๋ก๋๋ http://commons.apache.org/io/.
์๋ ์์ ๋ฅผ ํ ์คํธํ ๋์๋ maxFileSize ์ ์ค์ ๋ ํ์ผํฌ๊ธฐ๋ณด๋ค ์์ ํ์ผ๋ค๋ง ์ ๋ก๋๊ฐ ๊ฐ๋ฅํ๋ต๋๋ค.
c:\temp ์ c:\apache-tomcat-5.5.29\webapps\data ๋๋ ํ ๋ฆฌ๊ฐ ์๋์ง ํ์ธํ์๊ณ ์์ผ๋ฉด ์์ฑํด์ฃผ์ธ์.
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %> <%@ page import="javax.servlet.http.*" %> <%@ page import="org.apache.commons.fileupload.*" %> <%@ page import="org.apache.commons.fileupload.disk.*" %> <%@ page import="org.apache.commons.fileupload.servlet.*" %> <%@ page import="org.apache.commons.io.output.*" %> <% File file ; int maxFileSize = 5000 * 1024; int maxMemSize = 5000 * 1024; ServletContext context = pageContext.getServletContext(); String filePath = context.getInitParameter("file-upload"); // Verify the content type String contentType = request.getContentType(); if ((contentType.indexOf("multipart/form-data") >= 0)) { DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File("c:\\temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax( maxFileSize ); try{ // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>JSP File upload</title>"); out.println("</head>"); out.println("<body>"); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); String fileName = fi.getName(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if( fileName.lastIndexOf("\\") >= 0 ){ file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ; }else{ file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ; } fi.write( file ) ; out.println("Uploaded Filename: " + filePath + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); }catch(Exception ex) { System.out.println(ex); } }else{ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); } %>
์ด์ http://localhost:8080/UploadFile.htm ์ ์ ์ํด์ ํ ์คํธ ํด๋ณผ๊น์?
์ ๋๋ก ์๋ํ๋ค๋ฉด c:\apache-tomcat-5.5.29\webapps\data\ ๋๋ ํ ๋ฆฌ์ ํ์ผ์ด ์ ๋ก๋ ๋์ด์์ ๊ฑฐ์์~
Reference : http://www.tutorialspoint.com/jsp/jsp_file_uploading.htm
'๐ป Programming > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JSP] JSTL ( JSP Standard Tag Library ) (0) | 2019.02.15 |
---|---|
[JSP] Page Redirection ( ํ์ด์ง ๋ฆฌ๋๋ ์ ) (0) | 2019.02.15 |
[JSP] Session ( ์ธ์ ) (0) | 2019.02.15 |
[JSP] Cookies ( ์ฟ ํค ์ธํ , ์ฝ๊ธฐ, ์ญ์ ํ๊ธฐ) (0) | 2019.02.15 |
[JSP] Filters ( ํํฐ ์ฌ์ฉํ๊ธฐ ) (0) | 2019.02.15 |