fileList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Map" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>업로드 파일 리스트</h1>
<%
Map<String, String> map = (HashMap<String, String>)request.getAttribute("map");
Iterator<String> keys = map.keySet().iterator();
while(keys.hasNext()) {
String key = keys.next();
String value = map.get(key);
%>
업로드된 파일명 : <%=key %><br>
원본 파일명 : <%=value %>
<%} %>
</body>
</html>
MultiUploadServlet.java
package com.sw.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
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("/upload2.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();
HashMap<String, String> map = new HashMap<String, String>();
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);
map.put(file_name,ori_file_name);
}
request.setAttribute("map", map);
} catch (Exception e) {
System.out.print("예외 발생 : " + e);
}
RequestDispatcher dispatcher = request.getRequestDispatcher("fileList.jsp");
dispatcher.forward(request, response);
}
}