
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 froala.editor.utils;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.tika.Tika;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import com.mortennobel.imagescaling.AdvancedResizeOp;
import com.mortennobel.imagescaling.MultiStepRescaleOp;
import egovframework.com.cmm.util.EgovResourceCloseHelper;
import egovframework.rte.fdl.cmmn.exception.EgovBizException;
import froala.editor.Utils;
import net.coobird.thumbnailator.Thumbnails;
public class FileUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);
/**
* 첨부파일을 서버에 저장한다.
*
* @param files
* @param uploadDir
* @return
* @throws EgovBizException
*/
public static List<Map<String, Object>> writeUploadFile(List<MultipartFile> files, String uploadDir)
throws EgovBizException {
File saveFolder = new File(uploadDir);
saveFolder.setExecutable(true);
saveFolder.setReadable(true);
saveFolder.setWritable(true);
if (!saveFolder.exists() || saveFolder.isFile()) {
saveFolder.mkdirs();
}
List<Map<String, Object>> resultfiles = new ArrayList<Map<String, Object>>();
for (MultipartFile file : files) {
if (file.isEmpty())
continue;
String origin_file_nm = file.getOriginalFilename();
String mime = "";
Tika tika = new Tika();
try {
mime = tika.detect(file.getInputStream());
} catch (IOException e) {
LOGGER.error("fail to process FileUtil writeUploadFile : {}", e);
}
int index = origin_file_nm.lastIndexOf(".");
String file_extsn = file.getOriginalFilename().substring(index + 1);
Long file_size = file.getSize();
String sys_file_nm;
try {
sys_file_nm = Utils.generateUniqueString() + "." + file_extsn;
} catch (NoSuchAlgorithmException e) {
throw new EgovBizException("NoSuchAlgorithmException!");
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(uploadDir + sys_file_nm);
FileCopyUtils.copy(file.getInputStream(), fos);
} catch (IOException e) {
LOGGER.error("fail to process FileUtil writeUploadFile : {}", e);
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
LOGGER.error("fail to process FileUtil writeUploadFile : {}", e);
}
}
// 이미지 파일일 경우 썸네일 저장
String thumb_nm = null;
if (ImageCheck(mime)) {
thumb_nm = "thumb_" + sys_file_nm;
try {
Thumbnails.of(file.getInputStream()).size(147, 108).toFile(uploadDir + thumb_nm);
} catch (IOException e) {
LOGGER.error("fail to process FileUtil writeUploadFile : {}", e);
thumb_nm = sys_file_nm;
}
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("file_extsn", file_extsn);
map.put("origin_file_nm", origin_file_nm);
map.put("sys_file_nm", sys_file_nm);
map.put("uploadDir", uploadDir);
map.put("file_size", file_size);
map.put("mime", mime);
if (thumb_nm != null) {
map.put("thumb_nm", thumb_nm);
}
resultfiles.add(map);
}
return resultfiles;
};
public File convert(MultipartFile file) throws EgovBizException {
File convFile = null;
FileOutputStream fos = null;
try {
convFile = new File(file.getOriginalFilename());
fos = new FileOutputStream(convFile);
convFile.createNewFile();
fos.write(file.getBytes());
} catch (IOException e) {
LOGGER.error("fail to process FileUtil convert : {}", e);
} finally {
EgovResourceCloseHelper.close(fos);
}
return convFile;
}
public File multipartToFile(MultipartFile multipart) throws IllegalStateException, IOException {
File convFile = new File(multipart.getOriginalFilename());
multipart.transferTo(convFile);
return convFile;
}
public static String deleteFile(String filePath) {
File file = new File(filePath);
// 이미지 파일일 경우 썸네일 삭제
String mime = "";
Tika tika = new Tika();
try {
mime = tika.detect(file);
} catch (IOException e) {
LOGGER.error("fail to process FileUtil multipartToFile : {}", e);
}
String thumb_nm = null;
if (ImageCheck(mime)) {
String[] arr = filePath.split("/");
arr[arr.length - 1] = "thumb_" + arr[arr.length - 1];
thumb_nm = CommonUtil.arrayJoin("/", arr);
File thumb_file = new File(thumb_nm);
thumb_file.delete();
}
file.delete();
return filePath;
}
public static boolean ImageCheck(String mime) {
if (mime.equals("image/jpeg") || mime.equals("image/x-ms-bmp") || mime.equals("image/png")
|| mime.equals("image/gif")) {
return true;
} else {
return false;
}
}
/**
* 이미지 변환(회전)
*
* @Author : 임종호
* @Date : 2020. 6. 15.
* @Method Name : transRotate
* @return : String
*/
public static String transRotate(Integer rotate, BufferedImage oldImage, String path) {
BufferedImage newImage = null;
int oldType = oldImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : oldImage.getType();
if (180 == rotate) {
newImage = new BufferedImage(oldImage.getWidth(), oldImage.getHeight(), oldType);
} else {
newImage = new BufferedImage(oldImage.getHeight(), oldImage.getWidth(), oldType);
}
Graphics2D graphics = (Graphics2D) newImage.getGraphics();
graphics.rotate(Math.toRadians(rotate), newImage.getWidth() / 2, newImage.getHeight() / 2);
if (180 != rotate) {
graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2,
(newImage.getHeight() - oldImage.getHeight()) / 2); // 90, 270도일때만 사용
}
graphics.drawImage(oldImage, 0, 0, oldImage.getWidth(), oldImage.getHeight(), null);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new java.io.File(path + "_" + rotate));
ImageIO.write(newImage, "png", fos);
} catch (IOException e) {
LOGGER.error("fail to process FileUtil transRotate : {}", e);
} finally {
if (fos != null)
try {
fos.close();
} catch (IOException e) {
LOGGER.error(e.getCause().getMessage());
}
}
return path + "_" + rotate;
}
/**
* 이미지 리사이즈
*
* @Author : 임종호
* @Date : 2020. 9. 17.
* @Method Name : ImageResize
* @return : BufferedImage
*/
public static BufferedImage ImageResize(BufferedImage image, float stdrSize) {
int width = image.getWidth();
int height = image.getHeight();
float rate = (float) (Math.round(width / stdrSize * 1000) / 1000.0);
// 최소, 최대 비율 지정
if (rate < 1) {
rate = 1.0f;
} else if (rate > 4) {
rate = 4.0f;
}
// 이미지 리사이즈
MultiStepRescaleOp rescale = new MultiStepRescaleOp(Math.round(width / rate), Math.round(height / rate));
rescale.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Soft);
return rescale.filter(image, null);
}
/**
* PDF 파일을 읽어 이미지를 생성한다.
*
* @Author : 임종호
* @Date : 2020. 9. 18.
* @Method Name : imageWrite
* @return : void
*/
public static void pdfImageWrite(PDFRenderer pdfRenderer, int i, String imgFileName) {
try {
BufferedImage image;
image = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB);
ImageIO.write(FileUtil.ImageResize(image, 5000.0f), "png", new java.io.File(imgFileName));
} catch (IOException e) {
LOGGER.error("fail to process FileUtil pdfImageWrite : {}", e);
java.io.File file = new java.io.File(imgFileName);
if (file.exists()) {
file.delete();
}
}
}
}