142 lines
4.3 KiB
Plaintext
142 lines
4.3 KiB
Plaintext
package kr.co.i4way.manage.util;
|
|
|
|
import java.io.File;
|
|
import java.io.InputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
import kr.co.i4way.manage.model.FormBasedFileVo;
|
|
|
|
/**
|
|
* @Class Name : EgovFileUploadUtil.java
|
|
* @Description : Spring 기반 File Upload 유틸리티
|
|
* @Modification Information
|
|
*
|
|
* 수정일 수정자 수정내용
|
|
* ------- -------- ---------------------------
|
|
* 2009.08.26 한성곤 최초 생성
|
|
*
|
|
* @author 공통컴포넌트 개발팀 한성곤
|
|
* @since 2009.08.26
|
|
* @version 1.0
|
|
* @see
|
|
*/
|
|
public class FileUploadUtil extends FormBasedFileUtil {
|
|
/**
|
|
* 파일을 Upload 처리한다.
|
|
*
|
|
* @param request
|
|
* @param where
|
|
* @param maxFileSize
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public static List<FormBasedFileVo> uploadFiles(HttpServletRequest request, String where, long maxFileSize) throws Exception {
|
|
List<FormBasedFileVo> list = new ArrayList<FormBasedFileVo>();
|
|
|
|
MultipartHttpServletRequest mptRequest = (MultipartHttpServletRequest)request;
|
|
Iterator fileIter = mptRequest.getFileNames();
|
|
|
|
while (fileIter.hasNext()) {
|
|
MultipartFile mFile = mptRequest.getFile((String)fileIter.next());
|
|
|
|
FormBasedFileVo vo = new FormBasedFileVo();
|
|
|
|
String tmp = mFile.getOriginalFilename();
|
|
|
|
if (tmp.lastIndexOf("\\") >= 0) {
|
|
tmp = tmp.substring(tmp.lastIndexOf("\\") + 1);
|
|
}
|
|
|
|
vo.setFileName(tmp);
|
|
vo.setContentType(mFile.getContentType());
|
|
vo.setServerSubPath(getTodayString());
|
|
vo.setPhysicalName(getPhysicalFileName());
|
|
vo.setSize(mFile.getSize());
|
|
|
|
if (tmp.lastIndexOf(".") >= 0) {
|
|
vo.setPhysicalName(vo.getPhysicalName()); // 2012.11 KISA 보안조치
|
|
}
|
|
|
|
if (mFile.getSize() > 0) {
|
|
InputStream is = null;
|
|
|
|
try {
|
|
is = mFile.getInputStream();
|
|
saveFile(is, new File(WebUtil.filePathBlackList(where+SEPERATOR+vo.getServerSubPath()+SEPERATOR+vo.getPhysicalName())));
|
|
} finally {
|
|
if (is != null) {
|
|
is.close();
|
|
}
|
|
}
|
|
list.add(vo);
|
|
}
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
* 파일을 Upload 처리한다.
|
|
*
|
|
* @param request
|
|
* @param where
|
|
* @param maxFileSize
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public static List<FormBasedFileVo> uploadFiles2(HttpServletRequest request, String where, long maxFileSize) throws Exception {
|
|
List<FormBasedFileVo> list = new ArrayList<FormBasedFileVo>();
|
|
|
|
MultipartHttpServletRequest mptRequest = (MultipartHttpServletRequest)request;
|
|
Iterator fileIter = mptRequest.getFileNames();
|
|
|
|
String uploadPath = request.getSession().getServletContext().getRealPath("/");
|
|
String imgUploadPath = uploadPath + File.separator + where; // 이미지를 업로드할 폴더를 설정 = /uploadPath/where
|
|
|
|
while (fileIter.hasNext()) {
|
|
MultipartFile mFile = mptRequest.getFile((String)fileIter.next());
|
|
|
|
FormBasedFileVo vo = new FormBasedFileVo();
|
|
|
|
String tmp = mFile.getOriginalFilename();
|
|
|
|
if (tmp.lastIndexOf("\\") >= 0) {
|
|
tmp = tmp.substring(tmp.lastIndexOf("\\") + 1);
|
|
}
|
|
|
|
vo.setFileName(tmp);
|
|
vo.setContentType(mFile.getContentType());
|
|
vo.setPhysicalName(getPhysicalFileName());
|
|
vo.setSize(mFile.getSize());
|
|
|
|
if (tmp.lastIndexOf(".") >= 0) {
|
|
vo.setPhysicalName(vo.getPhysicalName()); // 2012.11 KISA 보안조치
|
|
}
|
|
|
|
if (mFile.getSize() > 0) {
|
|
InputStream is = null;
|
|
|
|
try {
|
|
is = mFile.getInputStream();
|
|
saveFile(is, new File(WebUtil.filePathBlackList(imgUploadPath+SEPERATOR+vo.getPhysicalName())));
|
|
} finally {
|
|
if (is != null) {
|
|
is.close();
|
|
}
|
|
}
|
|
list.add(vo);
|
|
}
|
|
}
|
|
|
|
return list;
|
|
}
|
|
} |