springMVC使用富文本编辑器kinditor时上传图片遇到的问题

    @RequestMapping(value="/kindEditorUpload")
    public void kindEditorUpload(HttpServletRequest request,HttpServletResponse response){
        JSONObject obj = new JSONObject();
        try {
            PropertiesUtil propertiesUtil=new PropertiesUtil();
            String ftpPath=propertiesUtil.readValue("FILE_DOWNLOAD_PATH");
            // 定义允许上传的文件扩展名  
            HashMap<String, String> extMap = new HashMap<String, String>();  
            extMap.put("image", "gif,jpg,jpeg,png,bmp");  
            MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;  
            Map<String, MultipartFile> fileMap = mRequest.getFileMap();  
            String fileName = null;  
            for (Iterator<Map.Entry<String, MultipartFile>> it = fileMap.entrySet().iterator(); it.hasNext();) {  
                  Map.Entry<String, MultipartFile> entry = it.next();  
                  MultipartFile mFile = entry.getValue();  
                  fileName = mFile.getOriginalFilename();  
                  System.out.println(fileName);
                  String fileExt = fileName.substring(fileName.lastIndexOf(".")+1);  
                  if (!Arrays.<String> asList(extMap.get("image").split(",")).contains(fileExt)) {  
                      obj.put("error", 1);
                      obj.put("message", "上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get("image") + "格式。");
                  }else{
                      FileSaveResult result=ftpFileAccess.saveFileToFTP(mFile, getFileName()+"."+fileExt);
                      System.out.println(result.getFileName()+"-----"+result.getFilePath());
                      obj.put("error", 0);
                      obj.put("url", ftpPath + result.getFilePath());
                      System.out.println(obj.toString());
                      returnKindEditorMsg(obj.toString(),request,response);
                  }
            }
        } catch (Exception e) {
            e.printStackTrace();
            obj.put("error", 1);
            obj.put("message", "上传文件失败,服务器错误!");
            returnKindEditorMsg(obj.toString(),request,response);
        }
    }
    private void returnKindEditorMsg(String returnMsg,HttpServletRequest  request, HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
//      response.setContentType("application/json; charset=UTF-8");
      try {
          response.getWriter().print(returnMsg);
          response.getWriter().flush();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }

在IE浏览器下响应的ContentType一定要定义为text/html,不然上传完图片后,会打开.json的文件

前台部分:

  var path="${pageContext.request.contextPath}";
  var editor;
  KindEditor.ready(function(K) {
        var url=path+'/wthdxx/kindEditorUpload';
      editor=K.create('#activityContent', {  
            resizeType : 0,
            width : this.width,
            height : this.height,
            uploadJson : url,
            allowFlashUpload:false,
            allowMediaUpload:false,
            allowImageRemote:false,
            cssData: 'body {font-family: "宋体"; font-size: 14px;line-height:14px;}',
            items : [
                     'source', '|', 'fullscreen', 'undo', 'redo', '|', 'preview', 'print', 'cut', 'copy', 'paste',
                     'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                     'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                     'superscript', 'clearhtml', 'quickformat','|', 'selectall', '/',
                     'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                     'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image',
                     'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
                     'anchor', 'link', 'unlink', '|', 'about'
             ],
            afterCreate : function() {
                var self = this;
                K.ctrl(document, 13, function() {
                    self.sync();
                    document.forms['example'].submit();
                });
                K.ctrl(self.edit.doc, 13, function() {
                    self.sync();
                    document.forms['example'].submit();
                });
            }
      });
  });
<form class="" name="example" method="post">
                                        <span> <i> <textarea id="activityContent"
                                                    name="content"
                                                    style="width:800px;height:400px;visibility:hidden;display: block;">KindEditor</textarea>
                                        </i> </span>
                                    </form>

猜你喜欢

转载自www.cnblogs.com/fpc-syq/p/9072845.html
今日推荐