νμΌ μ λ‘λ νΌ λ§λ€κΈ°
λ€μμ λ³Ό 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 |