바 이 두 편집기 ueditor 와 springmvc 프로젝트 통합

12942 단어 java-web
ueditor 가 제공 하 는 공식 예 는 spring 버 전이 없 으 며 jsp 버 전의 배경 에 따라 직접 사용 하 는 문제 가 많 습 니 다.배경 설정 파일 config.json 의 경 로 는 다른 설정 파일, 즉 src/main/resources 디 렉 터 리 에 놓 을 수 있 습 니 다.프론트 데스크 톱 설정 파일 ueditor.config.js 의 경 로 는 serverUrl : "/upload/dispatch" 으로 변경 되 었 습 니 다. 코드 는 다음 과 같 습 니 다.
@Controller
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private AttachmentService attachmentService;
    @RequestMapping("/dispatch")
    public void config(HttpServletRequest request, HttpServletResponse response, String action) {
        System.out.println("upload request:  " + action);
        response.setContentType("application/json");
        try {
            String exec = ActionEnter.getInstance(request, action).exec();
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }

    }
}

배경 에 있 는 파일 업로드 경 로 는 프로젝트 가 시 작 된 후에 수정 되 지 않 기 때문에 ConfigManager 를 단일 모드 ConfigManager. java 에서 수정 한 키 코드 는 다음 과 같 습 니 다.
private static ConfigManager instance;


public static ConfigManager getInstance(String rootPath, String contextPath, String uri) {

        try {
            if (instance == null)
                instance = new ConfigManager(rootPath, contextPath, uri);
            return instance;
        } catch (Exception e) {
            return null;
        }

    }

public static ConfigManager getInstance(){
        return instance;
    }

이에 따라 ActionEnter 의 초기 화도 수정 해 야 한다.
private ConfigManager configManager = null;

private ActionEnter(HttpServletRequest request) {

        this.request = request;
        this.rootPath = request.getSession().getServletContext().getRealPath("/");
        this.actionType = request.getParameter("action");
        this.contextPath = request.getContextPath();
        this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath,
                request.getRequestURI().replace(request.getContextPath(), ""));

    }

private ActionEnter(HttpServletRequest request, ConfigManager configManager) {
        this.request = request;
        this.actionType = request.getParameter("action");
        this.contextPath = request.getContextPath();
        this.configManager = configManager;
    }

public static ActionEnter getInstance(HttpServletRequest request,String action) {
        try {
            ConfigManager configManager = ConfigManager.getInstance();
            if (configManager == null||action.equals("config")) {
                return new ActionEnter(request);
            } else {
                return new ActionEnter(request, configManager);
            }
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }

제 가 이런 수정 을 한 것 은 주로 ueditor 에 올 리 지 않 은 파일 도 같은 배경 을 호출 하고 통 일 된 방식 으로 저장 할 수 있 도록 하기 위해 서 입 니 다.
예 를 들 어 fine uploader 를 사용 하여 그림 을 업로드 합 니 다. 요청 주 소 는 다음 과 같 습 니 다.
request : {
            endpoint : ''
        },

UploadController. 자바 에 서 는 ueditor 의 호출 방식 과 같 을 수 있 습 니 다.
파일 업로드 에 실패 한 문제 가 발생 하면 jamesMuWB 의 수정 을 참고 하여 Binary Uploader. java 의 save 방법 을 다시 쓰 십시오.제 가 jamesMuWB 를 바탕 으로 일부 코드 를 수정 한 것 은 업로드 경로 와 미리 보기 그림 의 경 로 를 분리 하기 위해 서 입 니 다.제 프로젝트 는 iis 서버 에 파일 을 저장 하기 때 문 입 니 다.
public static final State save(HttpServletRequest request, Map conf) {

        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartRequest.getFile(conf.get("fieldName").toString());

            String savePath = (String) conf.get("savePath");
            String originFileName = multipartFile.getOriginalFilename();
            String suffix = FileType.getSuffixByFilename(originFileName);

            originFileName = originFileName.substring(0, originFileName.length() - suffix.length());
            savePath = savePath + suffix;

            long maxSize = ((Long) conf.get("maxSize")).longValue();

            if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
                return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
            }

            savePath = PathFormat.parse(savePath, originFileName);
            String physicsPath = (String) conf.get("localPath") + savePath;
            State storageState = StorageManager.saveFileByInputStream(multipartFile.getInputStream(), physicsPath,
                    maxSize);
            String iisPath = (String) conf.get("iisUrl") + savePath;
            if (storageState.isSuccess()) {
                storageState.putInfo("url", PathFormat.format(iisPath));
                storageState.putInfo("type", suffix);
                storageState.putInfo("original", originFileName + suffix);
            }
            System.out.println(storageState.toJSONString());
            return storageState;

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return new BaseState(false, AppInfo.IO_ERROR);
    }

해당 config. json 도 수정 해 야 합 니 다.
 /*         */
    "iisUrl":"http://192.168.1.103:8082",
    "localPath":"E:/upload",
    "imageActionName": "uploadimage", /*        action   */
    "imageFieldName": "upfile", /*           */
    "imageMaxSize": 2048000, /*       ,  B */
    "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /*          */
    "imageCompressEnable": true, /*       ,   true */
    "imageCompressBorder": 1600, /*           */
    "imageInsertAlign": "none", /*           */
    "imageUrlPrefix": "", /*          */
    "imagePathFormat": "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /*       ,                */


마지막 문 제 는 ueditor 에서 원본 코드 를 바 꾸 거나 업로드 한 후에 다시 보면 내용 이 달라 집 니 다. 이것 은 버그 가 아 닙 니 다. 특정한 버 전부터 xs 필터 가 기본적으로 열 렸 습 니 다. 설정 만 수정 하면 됩 니 다.
    xssFilterRules : false
    // input xss  
    ,
    inputXssFilter : false
    // output xss  
    ,
    outputXssFilter : false

좋은 웹페이지 즐겨찾기