스프링 프레임워크(Spring Framework)
스프링 파일 업로드
xemaker
2017. 12. 5. 08:11
스프링 프레임워크 환경에서 파일 업로드 시 대략적인 흐름을 작성했습니다.
세부 사항은 추가하면서 작성하면 되겠습니다.
public Map<String, Object> fileUpload(HttpServletRequest request) throws Exception{
Map<String, Object> resultMap = new HashMap<String, Object>();
DateFormat dfFormat = new SimpleDateFormat("yyyyMMdd");
Date nowDate = new Date();
String toDate = format.dfFormat(nowDate);
String filePath = toDate+"/";
String fullPath=rootPath+filePath;
File file = new File(fullPath);
if(!file.isDirectory){
file.mkdirs();
}
MultipartHttpServletRequest multipart = null;
Enumeration enuParamName = null;
if(request.getContentType() != null && request.getContentType().indexOf("multipart/form-data") > -1){
multipart = (MultipartHttpServletRequest)request;
List<multipartFile> files = multipart.getFiles("");
//파라미터사용시
enuParamName = multipart.getParamterNames();
for(int j=0; j < files.size; j++){
multipartFile = files.get(j);
file = new File(pathFile);
multipartFile.transferTo(file);
paramMap.put("fileNo", fileNo);
}
}
}
세부 사항은 추가하면서 작성하면 되겠습니다.
public Map<String, Object> fileUpload(HttpServletRequest request) throws Exception{
Map<String, Object> resultMap = new HashMap<String, Object>();
DateFormat dfFormat = new SimpleDateFormat("yyyyMMdd");
Date nowDate = new Date();
String toDate = format.dfFormat(nowDate);
String filePath = toDate+"/";
String fullPath=rootPath+filePath;
File file = new File(fullPath);
if(!file.isDirectory){
file.mkdirs();
}
MultipartHttpServletRequest multipart = null;
Enumeration enuParamName = null;
if(request.getContentType() != null && request.getContentType().indexOf("multipart/form-data") > -1){
multipart = (MultipartHttpServletRequest)request;
List<multipartFile> files = multipart.getFiles("");
//파라미터사용시
enuParamName = multipart.getParamterNames();
for(int j=0; j < files.size; j++){
multipartFile = files.get(j);
file = new File(pathFile);
multipartFile.transferTo(file);
paramMap.put("fileNo", fileNo);
}
}
}