1. 程式人生 > >百度富文字編輯器ueditor使用小結

百度富文字編輯器ueditor使用小結

  
  /**
      *   圖片上傳
      * @author CK
      * @param upfile
      * @param request
      * @param response
      * @return
      * @throws Exception
      *
      */
     @RequestMapping("/upload/img")
     @ResponseBody
     public String upload(@RequestParam("upfile") MultipartFile upfile,HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String result = null;
        String originalName = upfile.getOriginalFilename();//返回客戶端檔案的原始名稱,eg:atv1.png

        String imgFileSuffix = "";//影象檔案的字尾
        String mimeType = upfile.getContentType();
        String supportedFormatTable[][] = {
            {"image/png", ".png"},
            {"image/bmp", ".bmp"},
            {"image/jpg", ".jpg"},
            {"image/gif", ".gif"},
            {"image/jpeg", ".jpg"}
        };

        for (int i = 0; i < supportedFormatTable.length; ++i) {
            if (supportedFormatTable[i][0].equals(mimeType)) {
              imgFileSuffix = supportedFormatTable[i][1];
                break;
            }
        }

        //檔案的字首,eg:201503271453000025
        String imgFilePrefix = new SimpleDateFormat("yyyyMMddHHmmssssss").format(Calendar.getInstance().getTime());
        String newImgFileName = imgFilePrefix + imgFileSuffix;

        String uploadPath = request.getSession().getServletContext().getRealPath("upload") + "/image";

        // 拼裝uuid
          String uuid = "img_" + imgFilePrefix;

          //刪除已存在的檔案
          File uploadedFile1 = new File(uploadPath, newImgFileName);

          if (uploadedFile1.exists()) {
              uploadedFile1.delete();
          }

          //儲存到資料庫以及儲存到相應的目錄檔案裡
          Resource oldResource = resourceService.get(uuid);

          if (oldResource != null) {
              File uploadedFile = new File(uploadPath, newImgFileName);
               org.apache.commons.io.FileUtils.writeByteArrayToFile(uploadedFile, upfile.getBytes());
               oldResource.setName(upfile.getOriginalFilename());
               oldResource.setFileSize(FileUtils.getHumanReadableFileSize(upfile.getSize()));
              oldResource.setUploadTime(new Date());
              oldResource.setUuid(uuid);
              oldResource.setStoreType(StoreType.IMAGE);
              resourceService.update(oldResource);
          } else {
              File uploadedFile = new File(uploadPath, newImgFileName);
               org.apache.commons.io.FileUtils.writeByteArrayToFile(uploadedFile, upfile.getBytes());
              Resource resource = new Resource();
              resource.setName(newImgFileName);
               resource.setFileSize(FileUtils.getHumanReadableFileSize(upfile.getSize()));
              resource.setUploadTime(new Date());
              resource.setUuid(uuid);
              resource.setStoreType(StoreType.IMAGE);
              resourceService.save(resource);
          }

          String projectUrl = "http://" + request.getServerName() //伺服器地址
                   + ":"
                   + request.getServerPort()           //埠號
                   + request.getContextPath();      //專案名稱

        //{"name":"10571402926855858.jpg", "originalName": "china.jpg", "size": 43086, "state": "SUCCESS", "type": ".jpg", "url": "upload/20140616/10571402926855858.jpg"}
        // result返回的url參照IndexController._file方法

        result = "{\"name\":\""+ newImgFileName +"\", \"originalName\": \""+ originalName +"\", \"size\": "+ upfile.getSize() +", \"state\": \"SUCCESS\", \"type\": \""+FileUtils.getFileExt(originalName)+"\", \"url\": \"" + projectUrl + "/upload/image/" + newImgFileName +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        return result;
    }