kindeditor上传文件在SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题 .

版权声明:本文为博主原创文章,未经博主允许不得转载。

最近用kindeditor做上传文件,老是在List items = upload.parseRequest(request);取不到值,查了一些资料有注释multipartResolver的,注释之后倒是好用,但是之前用multipartResolver做的上传就没法用了,自己修改后解决方法如下:
1.multipartResolver配置文件不用注释,也不用重写org.springframework.web.multipart.commons.CommonsMultipartResolver。
2.controller中的方法如下:

@RequestMapping("/uploadFile")
    public void uploadFile( HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map fileMap = multipartRequest.getFileMap();
        PrintWriter out = response.getWriter();
        //文件保存目录路径
//      String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
        String savePath = Global.UPLOAD_FOLDER + File.separator + "kindeditor/"; // 完整路径
        //文件保存目录URL
        String saveUrl  = request.getContextPath() + "/rest/por/kindeditorManager/show?templateName=";
//      String saveUrl  = Global.UPLOAD_FOLDER + File.separator + "kindeditor/";

        //定义允许上传的文件扩展名
        HashMap extMap = new HashMap();
        extMap.put("image", "gif,jpg,jpeg,png,bmp");
        extMap.put("flash", "swf,flv");
        extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
        extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

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

        response.setContentType("text/html; charset=UTF-8");

        if(!ServletFileUpload.isMultipartContent(request)){
            out.println(getError("请选择文件。"));
            return;
        }
        //检查目录
        File uploadDir = new File(savePath);
        if(!uploadDir.isDirectory()){
            //如果不存在,创建文件夹
            if (!uploadDir.exists()){
                uploadDir.mkdirs();
            }
            else{
                out.println(getError("上传目录不存在。"));
                return; 
            }
        }
        //检查目录写权限
        if(!uploadDir.canWrite()){
            out.println(getError("上传目录没有写权限。"));
            return;
        }

        String dirName = request.getParameter("dir");
        if (dirName == null) {
            dirName = "image";
        }
        if(!extMap.containsKey(dirName)){
            out.println(getError("目录名不正确。"));
            return;
        }
        //创建文件夹
        savePath += dirName + "/";
        saveUrl += dirName + "/";
        File saveDirFile = new File(savePath);
        if (!saveDirFile.exists()) {
            saveDirFile.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
        savePath += ymd + "/";
        saveUrl += ymd + "/";
        File dirFile = new File(savePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        //此处是直接采用Spring的上传
        for (Map.Entry entity : fileMap.entrySet()) {
            MultipartFile mf = entity.getValue();
            String fileFullname = mf.getOriginalFilename();
            fileFullname = fileFullname.replace('&', 'a');
            fileFullname = fileFullname.replace(',', 'b');
            fileFullname = fileFullname.replace(',', 'c');
            //检查扩展名
            String fileExt = fileFullname.substring(fileFullname.lastIndexOf(".") + 1).toLowerCase();
            if(!Arrays.asList(extMap.get(dirName).split(",")).contains(fileExt)){
                out.println(getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。"));
                return;
            }

            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;

            File uploadFile = new File(savePath + newFileName);
            try {
                FileCopyUtils.copy(mf.getBytes(), uploadFile);
                JSONObject obj = new JSONObject();
                obj.put("error", 0);
                obj.put("url", saveUrl + newFileName);
                out.println(obj.toJSONString());
            } catch (IOException e) {
                e.printStackTrace();
                out.println(getError("上传文件失败。"));
                return;
            }
        }
        //上传结束

    }

这样写就ok了希望对你有帮助。

你可能感兴趣的:(SpringMVC)