MarkDown编辑器

javaweb之MarkDown编辑器的嵌入

下载editor.md
实现拖拽图片上传,ctrl+v截图上传

目录结构

MarkDown编辑器_第1张图片
image.png

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


  $Title$
  
  
  
  
  
  


<%--div class editormd-theme-dark 设置上方工具栏主题--%>

servlet

package com.neu.servlet;

import com.alibaba.fastjson.JSON;
import com.neu.utils.MapRec;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "MapuploadServlet",urlPatterns = "/mapupload.do")
@MultipartConfig
public class MapuploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Part part = request.getPart("editormd-image-file");
        String path = request.getServletContext().getRealPath("")+"upload/";
        File file = new File(path);
        if (!file.exists()){
            file.mkdir();
        }
        UUID uuid = UUID.randomUUID();
        MapRec mapRec = new MapRec();
        mapRec.setSuccess(1);
        mapRec.setMessage("成功");
        mapRec.setName(part.getSubmittedFileName());
        mapRec.setUrl("upload/" + uuid + part.getSubmittedFileName());
        part.write(path + uuid + part.getSubmittedFileName());
        response.getWriter().print(JSON.toJSONString(mapRec));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet");
    }
}
package com.neu.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "GetValueServlet",urlPatterns = "/getvalue.do")
public class GetValueServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String doc = request.getParameter("test-editormd-markdown-doc");
        String html = request.getParameter("test-editormd-html-code");
        System.out.println(html);
        System.out.println(doc);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

MapRec类

package com.neu.utils;

public class MapRec {
    private int success;
    private String message;
    private String url;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSuccess() {
        return success;
    }

    public void setSuccess(int success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

Filter

package com.neu.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(filterName = "Filter01_Encode",urlPatterns = "/*")
public class Filter01_Encode implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

你可能感兴趣的:(MarkDown编辑器)