• 大小: 13KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-14
  • 语言: Java
  • 标签: Upload  KindEditor  

资源简介

Spring Boot (程序篇):集成富文本编辑器KindEditor自动生成HTML静态页面并保存,上传 UploadController

资源截图

代码片段和文件信息

package com.imenger.richlink.controller;

import com.fasterxml.jackson.databind.objectMapper;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lvyimeng
 * @version V1.0
 * @title: richlink
 * @Description: 上传
 * @date: 2018/10/9 18:38
 */
@Controller
public class UploadController {

    private static final objectMapper objectMapper = new objectMapper();
    private PrintWriter writer = null;

    @RequestMapping(value = “fileUpload“ method = RequestMethod.POST)
    public void fileUpload(MultipartHttpServletRequest request
                           HttpServletResponse response) throws ServletException IOException
            FileUploadException {
        ServletContext application = request.getSession().getServletContext();
        String savePath = application.getRealPath(“/“) + “attached/“;

        // 文件保存目录URL
        String saveUrl = request.getContextPath() + “/attached/“;

        // 定义允许上传的文件扩展名
        HashMap extMap = new HashMap();
        extMap.put(“image“ “gifjpgjpegpngbmp“);
        extMap.put(“flash“ “swfflv“);
        extMap.put(“media“ “swfflvmp3wavwmawmvmidavimpgasfrmrmvb“);
        extMap.put(“file“ “docdocxxlsxlsxppthtmhtmltxtziprargzbz2“);

        // 最大文件大小
        long maxSize = 1000000;

        response.reset();
        response.setCharacterEncoding(“UTF-8“);
        response.setContentType(“text/html“);
        writer = response.getWriter();
        // writer.println(json);  //想办法把map转成json

        if (!ServletFileUpload.isMultipartContent(request)) {
            writer.println(objectMapper.writeValueAsString(getError(“请选择文件。“)));
            return;

        }
        // 检查目录
        File uploadDir = new File(savePath);
        if (!uploadDir.isDirectory()) {
            writer.println(objectMapper.writeValueAsString(getError(“上传目录不存在。“)));
            return;
        }
        // 检查目录写权限
        if (!uploadDir.canWrite()) {
            writer.println(objectMapper.writeValueAsString(getError(“上传目录没有写权限。“)));
            return;
        }

        String dirName = request.getParameter(“dir“);
        if (dirName == null) {
            dirName = “image“;

评论

共有 条评论