파일 업로드

2021. 7. 4. 23:46(구)공부/JSP

728x90

파일 업로드를 위한 FORM 태그 준비

form 태그의 method 속성은 반드시 POST 방식으로 설정

form 태그의 enctype 속성은 반드시 multipart/form-data설정

form 태그의 action 속성은 파일 업로드를 처리할 JSP 파일로 설정

파일 업로드를 위해 input 태그의 type 속성을 file설정

만약 여러 파일을 업로드하려면 2개 이상의 input 태그를 사용하고 name 속성에 서로 다른 값을 설정

 

파일 업로드 처리 방법

MultipartRequest - 가장 간단한 방법. cos.jar 필요

아파치 API 이용 - 편리하고 강력한 API. commons-fileupload.jar , commons-io.jar

 

MultipartRequest

MultipartRequest multi = new MultipartRequest(request,"C:\\upload",5*1024*1024,"utf-8",new DafaultFileRenamePolicy())

request : 리퀘스트 내장 객체 설정

saveDirectory : 서버 파일 저장 경로

maxPostSize : 파일의 최대 크기(바이트 단위) 설정. 최대 크기 넘어가면 IOException 발생

encoding : 코딩 유형

policy : 파일명 중복되는 경우 처리 방식. 생략하면 덮어쓰기

 

메소드

getContentType(String name)  String 업로드된 콘텐츠 유형 반환 없으면 null

getParameter(String name) String 이름이 name인 파라미터 반환

getParameterNames() Enumeration 요청 파라미터 이름들을 Enumeration 타입으로 반환

getFile(String  name) File 파일 객체 반환 없으면 null

getFileNames() Enumeration 요청 폼 내에 type 이 file 인 요청 파라메터 이름들

getFileSystemName(String name) String 서버에 실제로 업로드된 이름 반환(이름 변경 정책에 따라)

getOriginalFileName(String name) String 정책에 따라 변경되기 전 원래 이름

 

Commons-FileUpload

파일 업로드 패키지

서버의 메모리상에서 파일 처리가 가능하도록 지원

DiskFileUpload 메소드

setRepositoryPath(String repositoryPath) void 업로드 된 파일을 임시 저장할 디렉토리

setSizeMax(long sizeMax) void 파일 최대 크기

setSizeThreshold(int sizeThreshold) void 메모리 상 저장할 최대 크기

parseRequest(HttpServeletRequest req) List<FileItem> multipart/form-data 유형의 요청 파라미터 가져오기

FileItem메소드

isFormField() boolean 요청 파라미터가 파일이 아닌 일반 데이터면 true

getFieldName() 요청 파라미터의 이름

getString() String 기본 문자 인코딩을 이용 요청 파라미터의 값

getString(String encoding)

getName() String 업로드된 파일의 이름

getSize() Long 파일의 사이즈

get() byte[] 파일을 바이트 배열로

isInMemory() boolean 파일이 메모리에 저장된 상태면 true 임시 디렉토리면 false

delete() void 파일과 관련된 자원 삭제

write() void 파일과 관련된 자원 저장

getContentType() String 브라우저가 전송하는 콘텐츠 유형을 반환 정의되어 있지 않으면 null

 

728x90

'(구)공부 > JSP' 카테고리의 다른 글

다국어처리 - 작성 대기  (0) 2021.07.05
유효성 검사 - 작성대기  (0) 2021.07.05
자바 빈즈  (0) 2021.07.04
태그  (0) 2021.07.04
내장 객체  (0) 2021.07.04