Filter를 이용한 multipart/form-data 처리 및 파일 업로드 처리
package com.xo.ddm.front.util.fileUtil;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public final class FileUploadFilter implements Filter{
public void init(FilterConfig aConfig) throws ServletException{
}
public void destroy(){
}
public void doFilter(
ServletRequest aRequest, ServletResponse aResponse, FilterChain aChain)
throws IOException, ServletException{
HttpServletRequest request = (HttpServletRequest) aRequest;
if(isFileUploadRequest(request)){
FileUploadWrapper wrapper = new FileUploadWrapper(request);
aChain.doFilter(wrapper, aResponse);
}else{
aChain.doFilter(aRequest, aResponse);
}
}
private boolean isFileUploadRequest(HttpServletRequest aRequest){
return
aRequest.getMethod().equalsIgnoreCase("POST") &&
aRequest.getContentType().startsWith("multipart/form-data");
}
}
package com.xo.ddm.front.util.fileUtil;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadWrapper extends HttpServletRequestWrapper{
/*
* 생성자 설정
*/
public FileUploadWrapper(HttpServletRequest aRequest)
throws IOException{
super(aRequest);
aRequest.setCharacterEncoding("UTF-8");
ServletFileUpload upload = new ServletFileUpload(
new DiskFileItemFactory());
try{
List<FileItem> fileItems = upload.parseRequest(aRequest);
convertToMaps(fileItems);
}catch (FileUploadException e){
//TODO Auto-generated catch block
throw new IOException("Cannot parse underlying request : "
+ e.toString());
}
}
/*
* 모든 파라메터 이름을 리턴 한다.(file type 필드 까지)
*/
@Override public Enumeration getParameterNames(){
Set<String> allNames = new LinkedHashSet<String>();
allNames.addAll(fRegularParams.keySet());
allNames.addAll(fFileParams.keySet());
return Collections.enumeration(allNames);
}
/*
* file type 필드가 아닌 모든 파라메터 값(value)를 리턴 한다.
*
* 파라메터가 존재 하지 않으면 null 리턴
* 파라메터가 존재 하나 값이 없으면 공백 리턴
* 파라메터가 여러개 일경우 최초의 값만 리턴
*/
@Override public String getParameter(String aName){
String result = null;
List<String> values = fRegularParams.get(aName);
if(values == null){
result = "";
}else if(values.isEmpty()){
result = "";
}else{
try{
result = new String(values.get(FIRST_VALUE).getBytes("ISO-8859-1"), "UTF-8");
}catch(UnsupportedEncodingException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
/*
* parameterValues[] 리턴
*/
@Override public String[] getParameterValues(String aName){
String[]result = null;
List<String> values = fRegularParams.get(aName);
if(values != null){
result = values.toArray(new String[values.size()]);
}
return result;
}
/*
* 파라메터 Map 리턴
*/
@Override public Map getParameterMap(){
return Collections.unmodifiableMap(fRegularParams);
}
/*
* fileItems 리턴
*/
public List<FileItem> getFileItems(){
return new ArrayList<FileItem>(fFileParams.values());
}
public FileItem getFileItem(String aFieldName){
return fFileParams.get(aFieldName);
}
private final Map<String, List<String>> fRegularParams = new
LinkedHashMap<String, List<String>>();
private final Map<String, FileItem>fFileParams = new
LinkedHashMap<String, FileItem>();
private static final int FIRST_VALUE = 0;
private void convertToMaps(List<FileItem> aFileItems){
for(FileItem item: aFileItems){
if(isFileUploadField(item)){
fFileParams.put(item.getFieldName(), item);
}else{
if(alreadyHasValue(item)){
addMultiValuedItem(item);
}else{
addSingleValueItem(item);
}
}
}
}
private boolean isFileUploadField(FileItem aFileItem){
return !aFileItem.isFormField();
}
private boolean alreadyHasValue(FileItem aItem){
return fRegularParams.get(aItem.getFieldName()) != null;
}
private void addSingleValueItem(FileItem aItem){
List<String> list = new ArrayList<String>();
list.add(aItem.getString());
fRegularParams.put(aItem.getFieldName(), list);
}
private void addMultiValuedItem(FileItem aItem){
List<String> values = fRegularParams.get(aItem.getFieldName());
values.add(aItem.getString());
}
}
'JAVA' 카테고리의 다른 글
[JAVA]filter에서 파라미터 받기 (1) | 2017.11.23 |
---|---|
[JAVA]Filter를 이용한 request 파라미터 변경 방법 (0) | 2017.11.23 |
[JAVA]filter 필터 (0) | 2017.11.23 |
[JAVA]filter 파일 업로드 -egov (0) | 2017.11.23 |
[JAVA]Filter 응용 (0) | 2017.11.21 |