필수 jar
- commons-fileupload-1.3.1.jar
- commons-io-2.4.jar
다운로드
Source
.
.
.
// 첨부파일 경로
String repositoryPath = request.getRealPath( "images/" );
// 메모리에 저장할 용량. KB단위
int threshold = 10 * 1024 * 1024;
// multipart
BoardMultipart multipart = new BoardMultipart( request , threshold , -1 , repositoryPath );
// requestWrapper에서 처리된 필드값을 request 에 입력함. 입력된 정보를 돌려받기 위해 jsp:useBean이 이 코드 아래에 위치해야함
HttpServletRequest tempRequest = request;
request = multipart;
.
.
.
// 포스트백일때
if( HelperRequest.isPostedBack( request ) ) {
// 파일 폼필드
Map< String , FileItem > fileDataMap = multipart.getFileItemMap();
// 첨부파일을 담을 이터레이터
Iterator< FileItem > fvi = null;
// 파일 폼필드가 있다면
if( fileDataMap != null ) {
// fileDataMap에서 파일 폼필드 데이터 추출
fvi = fileDataMap.values().iterator();
// fvi의 값이 있는지 없는지 확인. 있으면 true, 없으면 false를 리턴
while( fvi.hasNext() ) {
// fi에 fvi값 저장
FileItem fi = ( FileItem )fvi.next();
// 첨부된 파일의 이름으로 파일 유무 확인. 이름이 있다면
if( ! HelperString.isNullOrEmpty( fi.getName() ) ) {
.
.
.
}
fi.delete();
}
}
// 입력정보에 오류가 없으면
if( HelperString.isNullOrEmpty( errorMessage ) ) {
.
.
.
// 첨부파일을 서버에 저장
if( fileDataMap != null ) {
// fileDataMap에서 파일 폼필드 데이터 추출
fvi = fileDataMap.values().iterator();
// fvi의 값이 있는지 없는지 확인. 있으면 true, 없으면 false를 리턴
while( fvi.hasNext() ) {
// fi에 fvi값 저장
FileItem fi = ( FileItem )fvi.next();
// 첨부된 파일의 이름으로 파일 유무 확인. 이름이 있다면
if( ! HelperString.isNullOrEmpty( fi.getName() ) ) {
// 첨부파일
File uploadFile = new File( repositoryPath , fi.getName() );
// 동명 파일 처리
if ( uploadFile.exists() ) {
.
.
.
}
// 파일을 업로드 함
fi.write( uploadFile );
}
fi.delete();
}
}
.
.
.
<div class="category_wrapper">
<label for="attachfile_01">추가 파일 1</label>
<div class="input_wrapper">
<span class="guide">※ 수정을 통해 파일을 추가 할 수 있습니다.</span>
<input type="file" id="attachfile_01" name="attachfile_01" class="length90" />
</div>
</div>
<div class="category_wrapper">
<label for="attachfile_02">추가 파일 2</label>
<div class="input_wrapper">
<input type="file" id="attachfile_02" name="attachfile_02" class="length90" />
</div>
</div>
<div class="category_wrapper">
<label for="attachfile_03">추가 파일 3</label>
<div class="input_wrapper">
<input type="file" id="attachfile_03" name="attachfile_03" class="length90" />
</div>
</div>