132 lines
4.2 KiB
Java
132 lines
4.2 KiB
Java
package com.dbnt.faisp.config;
|
|
|
|
|
|
import com.dbnt.faisp.util.ParamMap;
|
|
import com.dbnt.faisp.util.Utils;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONObject;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.util.FileCopyUtils;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.InputStream;
|
|
import javax.imageio.ImageIO;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
|
@RestController
|
|
@RequiredArgsConstructor
|
|
public class EditorController extends BaseService{
|
|
|
|
@Value("${site.domain}")
|
|
protected String siteDomain;
|
|
|
|
@Value("${editor.img.view}")
|
|
protected String imgView;
|
|
|
|
|
|
@PostMapping("/Crosseditor/uploadImg")
|
|
public @ResponseBody JSONObject uploadImg(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
JSONObject data = new JSONObject();
|
|
JSONArray req_array = new JSONArray();
|
|
|
|
try {
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
|
|
MultipartFile mFile = multipartRequest.getFile("imageFile");
|
|
if(!"".equals(mFile.getOriginalFilename())){
|
|
|
|
String attach_file_Name = mFile.getOriginalFilename();
|
|
String attach_save_Name = Utils.generationSaveName();
|
|
String imageKind = request.getParameter("imageKind");
|
|
String editorFrame = request.getParameter("editorFrame");
|
|
|
|
//파일 타입
|
|
String extNm = attach_file_Name.substring( attach_file_Name.lastIndexOf( "." ) + 1);
|
|
if("jpg,png,jpeg".contains(extNm.toLowerCase())) {
|
|
|
|
File dir = new File(locationPath+editorPath);
|
|
if (!dir.exists()) {
|
|
try{
|
|
|
|
// 생성
|
|
boolean result2 = dir.mkdir();
|
|
if (result2) {
|
|
System.out.println("Directory is created.");
|
|
} else {
|
|
System.out.println("Failed to create directory.");
|
|
}
|
|
} catch(Exception e){
|
|
System.out.println("Exception occurred.");
|
|
e.getStackTrace();
|
|
}
|
|
} else {
|
|
System.out.println("Directory already exists");
|
|
}
|
|
|
|
File file = new File(locationPath+editorPath, attach_save_Name +"."+ extNm);
|
|
FileCopyUtils.copy(mFile.getBytes(), file);
|
|
InputStream is = new FileInputStream(file);
|
|
BufferedImage input = ImageIO.read(is);
|
|
if(input.getWidth()>720){
|
|
int width = 720;
|
|
int height = (int) (input.getHeight()*(720d/input.getWidth()));
|
|
|
|
Image resizeImage = input.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
|
BufferedImage output = new BufferedImage(width, height, input.getType());
|
|
Graphics graphics = output.createGraphics();
|
|
graphics.drawImage(resizeImage, 0, 0, null);
|
|
graphics.dispose();
|
|
ImageIO.write(output, extNm, file);
|
|
}
|
|
|
|
String webPath = siteDomain+ imgView + attach_save_Name +"."+ extNm;
|
|
|
|
jsonObject.put("result","success");
|
|
data.put("imageURL",webPath);
|
|
data.put("imageKind", imageKind);
|
|
data.put("editorFrame", editorFrame);
|
|
req_array.add(data);
|
|
jsonObject.put("addmsg", req_array);
|
|
|
|
} else {
|
|
ParamMap error = new ParamMap();
|
|
error.set("message", "Check File Extentions.");
|
|
}
|
|
} else {
|
|
ParamMap error = new ParamMap();
|
|
error.set("message", "Check File Extentions.");
|
|
|
|
}
|
|
} catch (Exception e) {
|
|
ParamMap error = new ParamMap();
|
|
error.set("message", "Check File Extentions.");
|
|
}
|
|
|
|
return jsonObject;
|
|
}
|
|
|
|
|
|
|
|
}
|