Пример #1
0
  @RequestMapping("/cropImg")
  @ResponseBody
  public Object cropImg(
      HttpServletRequest request,
      String imgUrl,
      double imgInitW,
      double imgInitH,
      double imgW,
      double imgH,
      double imgY1,
      double imgX1,
      double cropH,
      double cropW) {
    Map<String, Object> map = new HashMap<String, Object>();
    ServletContext context = request.getServletContext();
    try {
      String fileName = imgUrl.substring(imgUrl.lastIndexOf("/") + 1);
      String relpath =
          context.getRealPath("/").substring(0, context.getRealPath("/").indexOf(File.separator))
              + File.separator;
      String uppath = "download" + File.separator + "headIcon" + File.separator + fileName;
      String filePath = relpath + uppath;
      File oraginal = new File(filePath);
      File tmp = new File(filePath + "tmp");
      File file = new File(filePath.replace(".", "1."));
      ImageUtils.resize(oraginal, tmp, (int) imgW, (int) imgH, 1.0f);
      ImageUtils.cut(tmp, file, (int) imgX1, (int) imgY1, (int) cropW, (int) cropH);
      oraginal.delete();
      tmp.delete();
      HttpSession session = request.getSession(false);
      UserSession userSession = (UserSession) session.getAttribute("userSession");
      Param ossBucket = paramService.findByKey(Constants.OSS_BUCKET);
      String key = "user" + userSession.getUser().getUid() + ".png";
      ossService.headIconUpload(ossBucket.getTextValue(), file, "user/headIcon/", key);
      String url = imgUrl.replace(".", "1.");

      map.put("status", "success");
      map.put("url", url);
    } catch (Exception e) {
      map.put("status", "error");
      map.put("message", "文件切割异常");
      LOG.error("文件切割异常:", e);
    }
    return map;
  }
Пример #2
0
 @RequestMapping("/updateHeadIcon")
 @ResponseBody
 public Object updateHeadIcon(String url, HttpServletRequest request) {
   Map<String, Object> map = new HashMap<String, Object>();
   try {
     UserSession userSession = (UserSession) request.getSession(false).getAttribute("userSession");
     User user = userSession.getUser();
     String fileName = url.substring(url.lastIndexOf("/") + 1);
     ServletContext context = request.getServletContext();
     String relpath =
         context.getRealPath("/").substring(0, context.getRealPath("/").indexOf(File.separator))
             + File.separator;
     String uppath = "download" + File.separator + "headIcon" + File.separator + fileName;
     String filePath = relpath + uppath;
     File file = new File(filePath);
     Param ossBucket = paramService.findByKey(Constants.OSS_BUCKET);
     Param ossEndpoint = paramService.findByKey(Constants.OSS_ENDPOINT);
     Param ossUrl = paramService.findByKey(Constants.OSS_URL);
     String key = "user" + userSession.getUser().getUid() + ".png";
     ossService.headIconUpload(ossBucket.getTextValue(), file, "user/headIcon/", key);
     String newUrl =
         "http://"
             + (ossUrl == null || ossUrl.getTextValue() == null || "".equals(ossUrl)
                 ? ossBucket.getTextValue() + "." + ossEndpoint.getTextValue()
                 : ossUrl.getTextValue())
             + "/user/headIcon/"
             + key;
     user.setHeadIconLocal(newUrl);
     if (user.getHeadIconUsed() == User.HEADICON_LOCAL) {
       user.setHeadIconBig(newUrl);
       user.setHeadIconMid(newUrl);
       user.setHeadIconSmall(newUrl);
     }
     file.delete();
     userService.update(user);
     map.put("success", true);
   } catch (Exception e) {
     LOG.error("更新头像失败", e);
     map.put("success", false);
     map.put("message", "未知错误");
   }
   return map;
 }