1. COS 라이브러리 다운로드
- 다운로드 : https://mvnrepository.com/artifact/com.servlets/cos/05Nov2002
2. COS 라이브러리를 이용한 파일 업로드
- FileUpload.java
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 = "C:\\Users\\user\\Desktop\\JAVA\\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);
}
}
- 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>
- COS 라이브러리 한계점
여러 개의 파일 업로드 시, 개별적으로 이름을 지정해주는 것이 번거롭다.