喻健
2018-11-13 4e420c75e250f07c5f305d96cd6ddaf46f78b012
fanli/src/main/java/com/yeshi/fanli/controller/admin/UploadController.java
@@ -198,4 +198,45 @@
         
   }
   
   /**
    * 上传图片jsonp
    * @param file
    * @param out
    */
   @RequestNoLogin()
   @RequestMapping(value = "uploadPicture")
   public void uploadPicture(@RequestParam("file") CommonsMultipartFile file, Long uid,
         HttpServletResponse response, PrintWriter out) {
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Access-Control-Allow-Methods", "*");
      AdminUser admin = adminUserService.selectByPrimaryKey(uid);
      if (admin == null) {
         out.print(JsonUtil.loadFalseResult("当前账户验证失败"));
         return;
      }
      if (file == null) {
         out.print(JsonUtil.loadFalseResult("上传文件为空"));
         return;
      }
      try {
         InputStream inputStream = file.getInputStream();
         String contentType = file.getContentType();
         String type = contentType.substring(contentType.indexOf("/") + 1);
         // 上传文件相对位置
         String fileUrl=UUID.randomUUID().toString().replace("-", "") + "." + type;
         String uploadPath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
         out.print(JsonUtil.loadTrueResult(uploadPath));
      } catch (IOException e) {
         out.print(JsonUtil.loadFalseResult("上传图片失败"));
         e.printStackTrace();
      }
   }
}