Servlet - MultiUploadServlet.java
package kr.devinside.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletContext;
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("/multiupload.do")
public class MultiUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MultiUploadServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
String savePath = "upload";
int uploadFileSizeLimit = 5 * 1024 * 1024;
String encType = "UTF-8";
ServletContext context = getServletContext();
String uploadFilePath = context.getRealPath(savePath);
try {
MultipartRequest multi = new MultipartRequest(request,
uploadFilePath,
uploadFileSizeLimit,
encType,
new DefaultFileRenamePolicy());
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
String file = (String)files.nextElement();
String file_name = multi.getFilesystemName(file);
String ori_file_name = multi.getOriginalFileName(file);
out.print("<br>업로드된 파일명 : " + file_name);
out.print("<br>업로드된 파일명 : " + ori_file_name);
out.print("<hr>");
}
} catch (Exception e) {
System.out.print("예외 발생 : " + e);
}
}
}
JSP - multiUpload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Multi File Upload</title>
</head>
<body>
<form action="multiupload.do" method="post" enctype="multipart/form-data">
1.파일 지정하기 : <input type="file" name="uplaodFile01"><br>
2.파일 지정하기 : <input type="file" name="uplaodFile02"><br>
3.파일 지정하기 : <input type="file" name="uplaodFile03"><br>
<input type="submit" value="전송">
</form>
</body>
</html>
File Upload를 위한 jar File
'Web > JSP' 카테고리의 다른 글
오류 #2 - java.lang.NullPointerException (0) | 2015.03.05 |
---|---|
오류 #1 - ora-00911 문자가 부적합합니다 (0) | 2015.02.26 |
JSP #20 DBCP (0) | 2015.02.17 |
JSP #19 Oracle DB - #2 PreparedStatement (0) | 2015.02.15 |
JSP #18 Oracle DB - #1 SELECT (0) | 2015.02.15 |