multipart 파일체크
if(isFileUploadRequest((HttpServletRequest)request)){
HttpServletRequest httpServletRequest = new HttpServletRequestWrapper((HttpServletRequest)request);//(HttpServletRequest)request;
String flag ="T";
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(50 * 1024);
factory.setRepository(new File("/"));
ServletFileUpload upload = new ServletFileUpload(factory);
ArrayList fileItems;
try {
fileItems = (ArrayList)upload.parseRequest(httpServletRequest);
Iterator i = fileItems.iterator();
while ( i.hasNext () ) {
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () ){
if (fi.getName().indexOf(".")>-1){
if (!getExtCheck(fi.getName(), writeExt)){
flag ="F";
break;
}
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
//System.out.println(request.getAttribute("flag").toString());
if (flag.equals("F")){
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<html>");
out.println("<head>");
out.println("<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">");
out.println("<script type=\"text/javascript\">");
out.println("alert('등록하실수 없는 확장자입니다.');");
out.println("history.back();");
out.println("</script>");
out.println("</head>");
out.println("</html>");
out.flush();
out.close();
return;
}
aChain.doFilter(request, response);
}else{
aChain.doFilter(request, response);
}
public boolean getExtCheck(String value, String writeExt){
String[] file = value.toLowerCase().split("\\.");
String ext = file[file.length-1];
if (ext!=null && ext.length()>0 && writeExt.indexOf(ext)==-1){
return false;
}
return true;
}
'JAVA' 카테고리의 다른 글
[JAVA] 휴일 알아내기 (0) | 2021.06.25 |
---|---|
multipart/form-data 인지 체크, MultipartRequestWrapper 상속해서 수정 (2) | 2019.04.09 |
multipart 파라미터 받기 (0) | 2019.02.28 |
multipart 클래스 사용 예제 (0) | 2019.02.28 |
multipart 케스팅 (0) | 2019.02.28 |