1. Apache Commons 라이브러리 다운로드
- Apache Commons IO
- 다운로드 : https://mvnrepository.com/artifact/commons-io/commons-io/2.8.0
- Apache Commons FileUpload
- 다운로드 : https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload/1.3.3
2. Apache Commons 라이브러리를 활용한 파일 업로드
- 파일이름 중복되지 않게 하기
String origin = fi.getName();
String ext = origin.substring(origin.lastIndexOf("."));
UUID uuid = UUID.randomUUID();
String name = uuid + ext;
- 업로드 날짜 별로 폴더 생성하기
File currentDir = new File(saveDir);
File upPath = new File(currentDir + "\\" + getTodayStr());
if(!upPath.exists()) {
upPath.mkdirs();
}
fi.write(new File(upPath, name));
// 업로드 날짜 포매팅 해주는 메서드
private String getTodayStr() {
return new SimpleDateFormat("yyyy/MM/dd").format(System.currentTimeMillis());
}
- FileUpload2.java
package test;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet("/file2")
public class FileUpload2 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("form.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String saveDir = "C:\\Users\\user\\Desktop\\JAVA\\upload";
int size = 10 * 1024 * 1024;
File currentDir = new File(saveDir);
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(currentDir);
factory.setSizeThreshold(size);
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> items = upload.parseRequest(req);
for(FileItem fi : items) {
if(fi.isFormField()) {
System.out.println(fi.getFieldName() + " = " + fi.getString("utf-8"));
}
else {
System.out.println(fi.getFieldName());
String origin = fi.getName();
System.out.println(origin);
String ext = origin.substring(origin.lastIndexOf("."));
UUID uuid = UUID.randomUUID();
String name = uuid + ext;
System.out.println(fi.getSize());
File upPath = new File(currentDir + "\\" + getTodayStr());
if(!upPath.exists()) {
upPath.mkdirs();
}
fi.write(new File(upPath, name));
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private String getTodayStr() {
return new SimpleDateFormat("yyyy/MM/dd").format(System.currentTimeMillis());
}
}
-Form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="f" multiple>
<button>업로드</button>
</form>
</body>
</html>