이번에 다운 받을 건 cos 라이브러리
파일 업로드를 위한 라이브러리 ~
무려 2002년도꺼... 내가 3살이였다니
요 버전으로 다운 받아줍시다.
package test;
import java.io.IOException;
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("/file")
public class FileUpload 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 = "D:\\upload";
int size = 10 * 1024 * 1024;
MultipartRequest multi = new MultipartRequest(req, saveDir, size, "utf-8", new DefaultFileRenamePolicy());
String name = multi.getFilesystemName("f");
String origin = multi.getOriginalFileName("f");
long fileSize = multi.getFile("f").length();
System.out.println(name);
System.out.println(origin);
System.out.println(fileSize);
}
}
그 다음은 Apache Commons FileUpload, Commons io
업로드한 년도 폴더 안에 월 폴더 안에 날짜 폴더 안에 파일 넣기
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 = "D:\\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(fi.getName());
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());
}
}
'자바 풀스택 공부' 카테고리의 다른 글
Day 53. [JSP/Servlet] 첨부파일, 회원기능 구현 (0) | 2022.03.22 |
---|---|
Day 52. [JSP/Servlet] 페이지네이션, 게시판별 카테고리 (0) | 2022.03.21 |
Day 50. [JSP/Servlet] 커스텀 태그 (0) | 2022.03.18 |
Day 49. [JSP/Servlet] useBean (0) | 2022.03.16 |
Day 49. [JSP/Servlet] 쿠키 & 세션 (0) | 2022.03.16 |
댓글