오늘은 파일 업로드를 구현하는 학습을 하였습니다
Today I Learned
- File Upload in web
@MultipartConfig(
location="/tmp",//(저장될 디렉토리, 보통 사용하지 않음 나중에 경로 따로 설정함)
fileSizeThreshold=10241024,//(이 크기를 넘으면 임시디렉토리에 저장)
maxFileSize=102410245,//(파일 하나의 최대 크기)
maxRequestSize=1024102455//(한 요청의 최대 크기)
)
Part filePart = request.getPart("file");
filePart.getSubmittedFileName();//(getString()은 바이너리로 가져올 수 있음)
String pathTemp = request.getServletContext().getRealPath("/static/notice/2020/26");//서블릿들이 필요로 하는 공통 자원
File path = new File(pathTemp);
if(!path.exists())
path.mkdirs();
String filePath = pathTemp + File.separator + filePart.getSubmittedFileName();
InputStream fis = filePart.getInputStream();
FileOutputStream fos = new FileOutputStream(filePath);
int size = 0;
byte[] buf = new byte[1024];
while((size = fis.read(buf)) != -1) {
fos.write(buf, 0, size);
}
fos.close();
fis.close();
오늘은 파일 업로드에 대하여 학습하였습니다.
이상입니다.