CKeditor 붙여넣기 캡처

21583 단어 CKeditor

CKeditor 붙여넣기 캡처


CKeditor는 기본적으로 캡처 붙여넣기를 지원하지만, 우리가 설정해야 합니다

1. CKeditor 구성


CKeditor의 config.js에 다음 설정 추가
//             
config.uploadUrl="/home/uploadPasteImage";
  
//         
config.extraPlugins="imagepaste";

2. 백그라운드 코드

@PostMapping(value = "/uploadPasteImage")
public String uploadPasteImage(@RequestParam("upload") MultipartFile[] file, HttpServletRequest request,
                               HttpServletResponse response, HttpSession session) {

  LOG.info("         ...");

  response.setCharacterEncoding("UTF-8");

  String result = "";
  try {
    for (int i = 0; i < file.length; i++) {
      //     ,           
      String fileName = ImageUtil.uploadImage(imgTempSavePath, file[i]);

      //        
      String imgUrl = imgVisitPath+fileName;

      //      json
      result = "{\"uploaded\" : 1, \"fileName\" : \""+fileName+"\", \"url\":\""+imgUrl+"\" }";

      LOG.info("      ...");
      break;
    }
  } catch (Exception e) {

    LOG.error("      ,error:{}" + e.getMessage());

    //      json
    result = "{\"uploaded\" : 1, \"fileName\" : \"image\", \"url\":\"image\" , \"error\" : { \"message\":\"      \" } }";
  }

  return result;
}

ImageUtil.java
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 *      
 **/
public class ImageUtil {

    private final static Logger LOG = LoggerFactory.getLogger(ImageUtil.class);

    /**
     *        ,     
     * @return
     */
    public static String uploadImage(String tempSavePath, MultipartFile imgFile) throws Exception {

        Date date = new Date();
        String fileName = ((Long)date.getTime()).toString() + imgSuffix(imgFile.getOriginalFilename());

        FileOutputStream fos = null;
        try {
            String path = FileUtil.createChildPath();
            //     
            File file = FileUtil.createFile(tempSavePath+path, fileName);
            LOG.info("   :{}", file.getName());

            fos = new FileOutputStream(file);
            IOUtils.copy(imgFile.getInputStream(), fos);
            LOG.info("      ");

            return path+file.getName();
        } catch (IOException ex) {
            ex.printStackTrace();
            LOG.error("      ");
            throw ex;
        } catch (Exception ex) {
            ex.printStackTrace();
            LOG.info("      ");
            throw ex;
        } finally {
            try {
              if(fos!=null) {
                fos.close();
              }
            } catch(Exception e) {
              e.printStackTrace();
            }
        }
    }
    
	/**
     *        ,  ,  :.jpg
     * @param imgName
     * @return
     */
    public static String imgSuffix(String imgName) {

        if(StringUtils.isEmpty(imgName)) {
            LOG.error("     ");
            throw new RuntimeException("     ");
        }

        return imgName.substring(imgName.lastIndexOf("."));
    }

}


FileUtil.java
public class FileUtil {

    private final static Logger LOG = LoggerFactory.getLogger(FileUtil.class);
  
    /**
     *     
     * @param parentPath
     * @param fileName
     * @return
     * @throws IOException
     */
    public static File createFile(String parentPath, String fileName) {

        File parentFile = new File(parentPath);

        //         ,       
        if(!parentFile.exists()) {
            parentFile.mkdirs();
        }

        //     
        File file = new File(parentPath + fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            LOG.error("      ");
        }

        return file;
    }
}

이때 캡처를 풍부한 텍스트 상자에 직접 붙일 수 있다.

좋은 웹페이지 즐겨찾기