File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
package itn.com.cmm.service;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import egovframework.rte.fdl.idgnr.EgovIdGnrService;
import egovframework.rte.fdl.property.EgovPropertyService;
import itn.let.utl.fcc.service.EgovStringUtil;
import net.coobird.thumbnailator.Thumbnails;
/**
* @Class Name : EgovFileMngUtil.java
* @Description : 메시지 처리 관련 유틸리티
* @Modification Information
*
* 수정일 수정자 수정내용
* ------- -------- ---------------------------
* 2009.02.13 이삼섭 최초 생성
* 2011.08.31 JJY 경량환경 템플릿 커스터마이징버전 생성
*
* @author 공통 서비스 개발팀 이삼섭
* @since 2009. 02. 13
* @version 1.0
* @see
*
*/
@Component("EgovFileMngUtil")
public class EgovFileMngUtil {
public static final int BUFF_SIZE = 2048;
public static final int MAX_WIDTH = 740;
public static final int MAX_HEIGHT = 960;
@Resource(name = "propertiesService")
protected EgovPropertyService propertyService;
@Resource(name = "egovFileIdGnrService")
private EgovIdGnrService idgenService;
private static final Logger LOGGER = LoggerFactory.getLogger(EgovFileMngUtil.class);
/**
* 첨부파일에 대한 목록 정보를 취득한다.
*
* @param files
* @return
* @throws Exception
*/
public List<FileVO> parseFileInf(Map<String, MultipartFile> files, String KeyStr, int fileKeyParam, String atchFileId, String storePath, String isThumbFile) throws Exception {
int fileKey = fileKeyParam;
String storePathString = "";
String atchFileIdString = "";
if ("".equals(storePath) || storePath == null) {
storePathString = propertyService.getString("Globals.fileStorePath");
} else {
storePathString = propertyService.getString(storePath);
}
if ("".equals(atchFileId) || atchFileId == null) {
atchFileIdString = idgenService.getNextStringId();
} else {
atchFileIdString = atchFileId;
}
File saveFolder = new File(storePathString);
if (!saveFolder.exists() || saveFolder.isFile()) {
saveFolder.mkdirs();
}
// Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();
List<MultipartFile> tmp = new ArrayList<MultipartFile>(files.values());
ListIterator<MultipartFile> itr = tmp.listIterator(tmp.size());
MultipartFile file;
String filePath = "";
List<FileVO> result = new ArrayList<FileVO>();
FileVO fvo;
while (itr.hasPrevious()) {
// Entry<String, MultipartFile> entry = itr.next();
// file = entry.getValue();
file = itr.previous();
String orginFileName = file.getOriginalFilename();
//--------------------------------------
// 원 파일명이 없는 경우 처리
// (첨부가 되지 않은 input file type)
//--------------------------------------
if ("".equals(orginFileName)) {
continue;
}
////------------------------------------
int index = orginFileName.lastIndexOf(".");
String fileExt = orginFileName.substring(index + 1);
String newName = KeyStr + EgovStringUtil.getTimeStamp() + fileKey;
long _size = file.getSize();
if (!"".equals(orginFileName)) {
filePath = storePathString + File.separator + newName;
file.transferTo(new File(filePath));
}
String thumbName = "";
if(("thumbFile").equals(isThumbFile)) {
// file inputstream 으로 섬네일 생성후 저장
// BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage originalImage = ImageIO.read(file.getInputStream());
// BufferedImage thumbnail = Thumbnails.of(originalImage).scale(0.25).asBufferedImage(); // 해상도
BufferedImage thumbnail = Thumbnails.of(originalImage).size(360, 260).asBufferedImage(); // 사이즈
thumbName = newName + "_THUMB";
String thumFilePath = storePathString + File.separator + thumbName;
File thumbFile = new File(thumFilePath);
ImageIO.write(thumbnail, fileExt, thumbFile);
// 서버 파일로 섬네일 저장
// File destinationDir = new File(filePath);
// Thumbnails.of(filePath).size(200, 200).toFiles(saveFolder, Rename.PREFIX_DOT_THUMBNAIL); // 이미지 확장자 파일 필요
}
fvo = new FileVO();
fvo.setFileExtsn(fileExt);
fvo.setFileStreCours(storePathString);
fvo.setFileMg(Long.toString(_size));
fvo.setOrignlFileNm(orginFileName);
fvo.setStreFileNm(newName);
fvo.setAtchFileId(atchFileIdString);
fvo.setFileSn(String.valueOf(fileKey));
fvo.setThumbFileNm(thumbName);
// writeFile(file, newName, storePathString);
result.add(fvo);
fileKey++;
}
return result;
}
/**
* 이미지 파일에 대한 목록 정보를 취득한다.
*
* @param files
* @return
* @throws Exception
*/
public List<FileVO> parseImageFileInf(Map<String, MultipartFile> files, String KeyStr, int fileKeyParam, String atchFileId, String storePath, String isThumbFile) throws Exception {
int fileKey = fileKeyParam;
String storePathString = "";
String atchFileIdString = "";
if ("".equals(storePath) || storePath == null) {
storePathString = propertyService.getString("Globals.fileStorePath");
} else {
storePathString = storePath;//propertyService.getString(storePath);
}
if ("".equals(atchFileId) || atchFileId == null) {
atchFileIdString = idgenService.getNextStringId();
} else {
atchFileIdString = atchFileId;
}
File saveFolder = new File(storePathString);
if (!saveFolder.exists() || saveFolder.isFile()) {
saveFolder.mkdirs();
}
// Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();
List<MultipartFile> tmp = new ArrayList<MultipartFile>(files.values());
ListIterator<MultipartFile> itr = tmp.listIterator(tmp.size());
MultipartFile file;
String filePath = "";
List<FileVO> result = new ArrayList<FileVO>();
FileVO fvo;
while (itr.hasPrevious()) {
// Entry<String, MultipartFile> entry = itr.next();
// file = entry.getValue();
file = itr.previous();
String orginFileName = file.getOriginalFilename();
//--------------------------------------
// 원 파일명이 없는 경우 처리
// (첨부가 되지 않은 input file type)
//--------------------------------------
if ("".equals(orginFileName)) {
continue;
}
////------------------------------------
int index = orginFileName.lastIndexOf(".");
String fileExt = orginFileName.substring(index + 1);
String newName = KeyStr + EgovStringUtil.getTimeStamp() + fileKey;
long _size = file.getSize();
//이미지 파일 처리라서 확장자까지 모두 붙여서 파일 생성 함
if (!"".equals(orginFileName)) {
filePath = saveFolder + File.separator + newName + "." + fileExt;
file.transferTo(new File(filePath));
}
//로컬 테스트 주석처리_220503_이준호
//실제 파일 저장 후 이미지 불러올 상대경로로 변경해줌 - webapps/mjon/MMS/날짜별 폴더
/*String fdlPath[] = storePathString.split("/MMS/"); // 경로중에 날짜 폴더명만 받아옴
storePathString ="/MMS/" + fdlPath[1];*/
String thumbName = "";
if(("thumbFile").equals(isThumbFile)) {
// file inputstream 으로 섬네일 생성후 저장
// BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage originalImage = ImageIO.read(file.getInputStream());
// BufferedImage thumbnail = Thumbnails.of(originalImage).scale(0.25).asBufferedImage(); // 해상도
BufferedImage thumbnail = Thumbnails.of(originalImage).size(360, 260).asBufferedImage(); // 사이즈
thumbName = newName + "_THUMB";
String thumFilePath = storePathString + File.separator + thumbName;
File thumbFile = new File(thumFilePath);
ImageIO.write(thumbnail, fileExt, thumbFile);
// 서버 파일로 섬네일 저장
// File destinationDir = new File(filePath);
// Thumbnails.of(filePath).size(200, 200).toFiles(saveFolder, Rename.PREFIX_DOT_THUMBNAIL); // 이미지 확장자 파일 필요
}
fvo = new FileVO();
fvo.setFileExtsn(fileExt);
fvo.setFileStreCours(storePathString);
fvo.setFileMg(Long.toString(_size));
fvo.setOrignlFileNm(orginFileName);
fvo.setStreFileNm(newName);
fvo.setAtchFileId(atchFileIdString);
fvo.setFileSn(String.valueOf(fileKey));
fvo.setThumbFileNm(thumbName);
// writeFile(file, newName, storePathString);
result.add(fvo);
fileKey++;
}
return result;
}
/**
* 이미지 파일을 리사이징하여 저장한다.
*
* @param files
* @return
* @throws Exception
*/
public List<FileVO> parseImageFileResizeInf(Map<String, MultipartFile> files, String KeyStr, int fileKeyParam, String atchFileId, String storePath, String isThumbFile) throws Exception {
int fileKey = fileKeyParam;
String storePathString = "";
String atchFileIdString = "";
if ("".equals(storePath) || storePath == null) {
storePathString = propertyService.getString("Globals.fileStorePath");
} else {
storePathString = storePath;//propertyService.getString(storePath);
}
if ("".equals(atchFileId) || atchFileId == null) {
atchFileIdString = idgenService.getNextStringId();
} else {
atchFileIdString = atchFileId;
}
File saveFolder = new File(storePathString);
if (!saveFolder.exists() || saveFolder.isFile()) {
saveFolder.mkdirs();
}
// Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();
List<MultipartFile> tmp = new ArrayList<MultipartFile>(files.values());
ListIterator<MultipartFile> itr = tmp.listIterator(tmp.size());
MultipartFile file;
String filePath = "";
List<FileVO> result = new ArrayList<FileVO>();
FileVO fvo;
while (itr.hasPrevious()) {
// Entry<String, MultipartFile> entry = itr.next();
// file = entry.getValue();
file = itr.previous();
String orginFileName = file.getOriginalFilename();
//--------------------------------------
// 원 파일명이 없는 경우 처리
// (첨부가 되지 않은 input file type)
//--------------------------------------
if ("".equals(orginFileName)) {
continue;
}
////------------------------------------
int index = orginFileName.lastIndexOf(".");
String fileExt = orginFileName.substring(index + 1);
String newName = KeyStr + EgovStringUtil.getTimeStamp() + fileKey;
long _size = file.getSize();
//이미지 파일 처리라서 확장자까지 모두 붙여서 파일 생성 함
if (!"".equals(orginFileName)) {
filePath = saveFolder + File.separator + newName + "." + fileExt;
file.transferTo(new File(filePath));
File newFile = new File(filePath);
InputStream inputStream = new FileInputStream(newFile);
Image img = new ImageIcon(newFile.toString()).getImage(); // 파일 정보 추출
int orgWidth = img.getWidth(null);
int orgHeight = img.getHeight(null);
//wid : 640 , hei : 960
if(orgWidth > 640 || orgHeight > 960) {
if (orgWidth > MAX_WIDTH) {
orgHeight = (int) (orgHeight * (MAX_WIDTH / (float) orgWidth));
orgWidth = MAX_WIDTH;
}
if (orgHeight > MAX_HEIGHT) {
orgWidth = (int) (orgWidth * (MAX_HEIGHT / (float) orgHeight));
orgHeight = MAX_HEIGHT;
}
//이미지 리사이징 요청
BufferedImage resizedImage = resize(inputStream ,orgWidth , orgHeight );
//리사이징된 파일 덮어쓰기
ImageIO.write(resizedImage, "jpg", new File(filePath));
}
}
//로컬 테스트 주석처리_220503_이준호
//실제 파일 저장 후 이미지 불러올 상대경로로 변경해줌 - webapps/mjon/MMS/날짜별 폴더
/*String fdlPath[] = storePathString.split("/MMS/"); // 경로중에 날짜 폴더명만 받아옴
storePathString ="/MMS/" + fdlPath[1];*/
String thumbName = "";
fvo = new FileVO();
fvo.setFileExtsn(fileExt);
fvo.setFileStreCours(storePathString);
fvo.setFileMg(Long.toString(_size));
fvo.setOrignlFileNm(orginFileName);
fvo.setStreFileNm(newName);
fvo.setAtchFileId(atchFileIdString);
fvo.setFileSn(String.valueOf(fileKey));
fvo.setThumbFileNm(thumbName);
// writeFile(file, newName, storePathString);
result.add(fvo);
fileKey++;
}
return result;
}
/* 리사이즈 실행 메소드 */
public static BufferedImage resize(InputStream inputStream, int width, int height)
throws IOException {
//BufferedImage inputImage = ImageIO.read(inputStream); // 받은 이미지 읽기
BufferedInputStream bis = new BufferedInputStream(inputStream, 8192); //8K reads
BufferedImage inputImage = ImageIO.read(bis); // 받은 이미지 읽기
//화질저하를 막기위해 이미지 SCALE_SMOOTH 처리를 해준다.
Image resizing = inputImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 입력받은 리사이즈 길이와 높이
Graphics2D graphics2D = outputImage.createGraphics();
//이미지 앨리어싱 현상이나 깨짐현상을 막기 위해서 INTERPOLATION 옵션을 사용
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics2D.drawImage(resizing, 0, 0, width, height, null); // 그리기
graphics2D.dispose(); // 자원해제
return outputImage;
}
/**
* 첨부파일을 서버에 저장한다.
*
* @param file
* @param newName
* @param stordFilePath
* @throws Exception
*/
protected void writeUploadedFile(MultipartFile file, String newName, String stordFilePath) throws Exception {
InputStream stream = null;
OutputStream bos = null;
String stordFilePathReal = (stordFilePath==null?"":stordFilePath).replaceAll("..","");
try {
stream = file.getInputStream();
File cFile = new File(stordFilePathReal);
if (!cFile.isDirectory()) {
boolean _flag = cFile.mkdir();
if (!_flag) {
throw new IOException("Directory creation Failed ");
}
}
bos = new FileOutputStream(stordFilePathReal + File.separator + newName);
int bytesRead = 0;
byte[] buffer = new byte[BUFF_SIZE];
while ((bytesRead = stream.read(buffer, 0, BUFF_SIZE)) != -1) {
bos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException fnfe) {
LOGGER.debug("fnfe: {}", fnfe);
} catch (IOException ioe) {
LOGGER.debug("ioe: {}", ioe);
} catch (Exception e) {
LOGGER.debug("e: {}", e);
} finally {
if (bos != null) {
try {
bos.close();
} catch (Exception ignore) {
LOGGER.debug("IGNORED: {}", ignore.getMessage());
}
}
if (stream != null) {
try {
stream.close();
} catch (Exception ignore) {
LOGGER.debug("IGNORED: {}", ignore.getMessage());
}
}
}
}
/**
* 서버의 파일을 다운로드한다.
*
* @param request
* @param response
* @throws Exception
*/
public static void downFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
String downFileName = EgovStringUtil.isNullToString(request.getAttribute("downFile")).replaceAll("..","");
String orgFileName = EgovStringUtil.isNullToString(request.getAttribute("orgFileName")).replaceAll("..","");
/*if ((String)request.getAttribute("downFile") == null) {
downFileName = "";
} else {
downFileName = EgovStringUtil.isNullToString(request.getAttribute("downFile"));
}*/
/*if ((String)request.getAttribute("orgFileName") == null) {
orgFileName = "";
} else {
orgFileName = (String)request.getAttribute("orginFile");
}*/
File file = new File(downFileName);
if (!file.exists()) {
throw new FileNotFoundException(downFileName);
}
if (!file.isFile()) {
throw new FileNotFoundException(downFileName);
}
byte[] b = new byte[BUFF_SIZE]; //buffer size 2K.
String fName = (new String(orgFileName.getBytes(), "UTF-8")).replaceAll("\r\n","");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition:", "attachment; filename=" + fName);
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
BufferedInputStream fin = null;
BufferedOutputStream outs = null;
try {
fin = new BufferedInputStream(new FileInputStream(file));
outs = new BufferedOutputStream(response.getOutputStream());
int read = 0;
while ((read = fin.read(b)) != -1) {
outs.write(b, 0, read);
}
} finally {
if (outs != null) {
try {
outs.close();
} catch (Exception ignore) {
LOGGER.debug("IGNORED: {}", ignore.getMessage());
}
}
if (fin != null) {
try {
fin.close();
} catch (Exception ignore) {
LOGGER.debug("IGNORED: {}", ignore.getMessage());
}
}
}
}
/**
* 첨부로 등록된 파일을 서버에 업로드한다.
*
* @param file
* @return
* @throws Exception
public static HashMap<String, String> uploadFile(MultipartFile file) throws Exception {
HashMap<String, String> map = new HashMap<String, String>();
//Write File 이후 Move File????
String newName = "";
String stordFilePath = EgovProperties.getProperty("Globals.fileStorePath");
String orginFileName = file.getOriginalFilename();
int index = orginFileName.lastIndexOf(".");
//String fileName = orginFileName.substring(0, _index);
String fileExt = orginFileName.substring(index + 1);
long size = file.getSize();
//newName 은 Naming Convention에 의해서 생성
newName = EgovStringUtil.getTimeStamp() + "." + fileExt;
writeFile(file, newName, stordFilePath);
//storedFilePath는 지정
map.put(Globals.ORIGIN_FILE_NM, orginFileName);
map.put(Globals.UPLOAD_FILE_NM, newName);
map.put(Globals.FILE_EXT, fileExt);
map.put(Globals.FILE_PATH, stordFilePath);
map.put(Globals.FILE_SIZE, String.valueOf(size));
return map;
}
*/
/**
* 파일을 실제 물리적인 경로에 생성한다.
*
* @param file
* @param newName
* @param stordFilePath
* @throws Exception
*/
protected static void writeFile(MultipartFile file, String newName, String stordFilePath) throws Exception {
InputStream stream = null;
OutputStream bos = null;
newName = EgovStringUtil.isNullToString(newName).replaceAll("..", "");
stordFilePath = EgovStringUtil.isNullToString(stordFilePath).replaceAll("..", "");
try {
stream = file.getInputStream();
File cFile = new File(stordFilePath);
if (!cFile.isDirectory())
cFile.mkdir();
bos = new FileOutputStream(stordFilePath + File.separator + newName);
int bytesRead = 0;
byte[] buffer = new byte[BUFF_SIZE];
while ((bytesRead = stream.read(buffer, 0, BUFF_SIZE)) != -1) {
bos.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException fnfe) {
LOGGER.debug("fnfe: {}",fnfe);
} catch (IOException ioe) {
LOGGER.debug("ioe: {}", ioe);
} catch (Exception e) {
LOGGER.debug("e: {}", e);
} finally {
if (bos != null) {
try {
bos.close();
} catch (Exception ignore) {
LOGGER.debug("IGNORED: {}", ignore.getMessage());
}
}
if (stream != null) {
try {
stream.close();
} catch (Exception ignore) {
LOGGER.debug("IGNORED: {}", ignore.getMessage());
}
}
}
}
/**
* 서버 파일에 대하여 다운로드를 처리한다.
*
* @param response
* @param streFileNm
* : 파일저장 경로가 포함된 형태
* @param orignFileNm
* @throws Exception
*/
public void downFile(HttpServletResponse response, String streFileNm, String orignFileNm) throws Exception {
// String downFileName = EgovStringUtil.isNullToString(request.getAttribute("downFile")).replaceAll("..","");
// String orgFileName = EgovStringUtil.isNullToString(request.getAttribute("orgFileName")).replaceAll("..","");
String downFileName = EgovStringUtil.isNullToString(streFileNm).replaceAll("..","");
String orgFileName = EgovStringUtil.isNullToString(orignFileNm).replaceAll("..","");
File file = new File(downFileName);
//log.debug(this.getClass().getName()+" downFile downFileName "+downFileName);
//log.debug(this.getClass().getName()+" downFile orgFileName "+orgFileName);
if (!file.exists()) {
throw new FileNotFoundException(downFileName);
}
if (!file.isFile()) {
throw new FileNotFoundException(downFileName);
}
//byte[] b = new byte[BUFF_SIZE]; //buffer size 2K.
int fSize = (int)file.length();
if (fSize > 0) {
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
String mimetype = "text/html"; //"application/x-msdownload"
response.setBufferSize(fSize);
response.setContentType(mimetype);
response.setHeader("Content-Disposition:", "attachment; filename=" + orgFileName);
response.setContentLength(fSize);
//response.setHeader("Content-Transfer-Encoding","binary");
//response.setHeader("Pragma","no-cache");
//response.setHeader("Expires","0");
FileCopyUtils.copy(in, response.getOutputStream());
} finally {
if (in != null) {
try {
in.close();
} catch (Exception ignore) {
LOGGER.debug("IGNORED: {}", ignore.getMessage());
}
}
}
response.getOutputStream().flush();
response.getOutputStream().close();
}
/*
String uploadPath = propertiesService.getString("fileDir");
File uFile = new File(uploadPath, requestedFile);
int fSize = (int) uFile.length();
if (fSize > 0) {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(uFile));
String mimetype = "text/html";
response.setBufferSize(fSize);
response.setContentType(mimetype);
response.setHeader("Content-Disposition", "attachment; filename=\""
+ requestedFile + "\"");
response.setContentLength(fSize);
FileCopyUtils.copy(in, response.getOutputStream());
in.close();
response.getOutputStream().flush();
response.getOutputStream().close();
} else {
response.setContentType("text/html");
PrintWriter printwriter = response.getWriter();
printwriter.println("<html>");
printwriter.println("<br><br><br><h2>Could not get file name:<br>" + requestedFile + "</h2>");
printwriter.println("<br><br><br><center><h3><a href='javascript: history.go(-1)'>Back</a></h3></center>");
printwriter.println("<br><br><br>© webAccess");
printwriter.println("</html>");
printwriter.flush();
printwriter.close();
}
//*/
/*
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition:", "attachment; filename=" + new String(orgFileName.getBytes(),"UTF-8" ));
response.setHeader("Content-Transfer-Encoding","binary");
response.setHeader("Pragma","no-cache");
response.setHeader("Expires","0");
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());
int read = 0;
while ((read = fin.read(b)) != -1) {
outs.write(b,0,read);
}
log.debug(this.getClass().getName()+" BufferedOutputStream Write Complete!!! ");
outs.close();
fin.close();
//*/
}
/**
* atchFileId 강제 Idgen update
*
* @param files
* @return
* @throws Exception
*/
public void updateAtchFileIdgen(int count) throws Exception {
for(int i = 0 ; i < count ; i ++) {
idgenService.getNextStringId();
}
}
}