예제 #1
0
 @Override
 public String convertImgUrl(int size, String imgUrl) {
   try {
     List<AppboxImg> appboxImgList = appboxImgDao.findByProperty("imgUrl", imgUrl);
     if (appboxImgList.isEmpty()) {
       // imgUrl is "" or not exists in the appboxImg table
       return "";
     }
     String relativePath = "";
     switch (size) {
       case 0:
         relativePath = appboxImgList.get(0).getTinyPath();
         break;
       case 1:
         relativePath = appboxImgList.get(0).getMiddlePath();
         break;
       default:
         // TODO expand later
         relativePath = appboxImgList.get(0).getOriginPath();
     }
     return relativePath;
   } catch (DataAccessException dse) {
     log.error(dse.toString());
     throw new ServiceException("DataAccessException", dse);
   }
 }
예제 #2
0
 /**
  * verify whether the image has been stored.
  *
  * @param imgUrl
  * @return
  */
 private boolean checkImgUrl(String imgUrl) {
   if (imgUrl.equals("")) {
     return true;
   }
   // TODO optimize this by adding 'limit 1'
   List<AppboxImg> appboxImgList = appboxImgDao.findByProperty("imgUrl", imgUrl);
   if (appboxImgList.isEmpty()) {
     return false;
   } else {
     return true;
   }
 }
예제 #3
0
  private void storeImg(String url) {
    // 计算文件名前缀
    String fileNamePrefix =
        url.substring(0, url.length() - 3)
            .replaceAll("http://", "")
            .replaceAll("\\.", "")
            .replaceAll("/", "");
    // 计算上传路径
    String outputPath = "";
    try {
      Calendar calendar = Calendar.getInstance();
      outputPath =
          ServerConfig.get("file_save_path")
              + File.separator
              + calendar.get(Calendar.YEAR)
              + File.separator
              + calendar.get(Calendar.MONTH)
              + File.separator
              + calendar.get(Calendar.DAY_OF_MONTH)
              + File.separator
              + calendar.get(Calendar.HOUR_OF_DAY)
              + File.separator;
      File file = new File(ServerConfig.get("real_root_path") + outputPath);
      file.mkdirs();
      log.info(file.toString());
    } catch (Exception e) {
      log.error("can't create output path");
      return;
    }

    try {
      String filePath = ServerConfig.get("real_root_path") + outputPath + fileNamePrefix;

      BufferedImage originalImage = ImageIO.read(new URL(url));
      // set image file storage path
      // TODO
      ImageIO.write(originalImage, "jpg", new File(filePath + ".jpg"));
      // start to scale the image
      ImageResizer imageResizer = new ImageResizer();
      imageResizer.setWidth(110);
      imageResizer.setHeight((int) (110 * originalImage.getHeight() / originalImage.getWidth()));
      imageResizer.setOriginalImage(originalImage);
      imageResizer.execute();
      BufferedImage resizedImage = imageResizer.getResizedImage();
      ImageIO.write(resizedImage, "jpg", new File(filePath + "0.jpg"));

      // imageResizer = new ImageResizer();
      imageResizer.setWidth(210);
      imageResizer.setHeight((int) (210 * originalImage.getHeight() / originalImage.getWidth()));
      imageResizer.setOriginalImage(originalImage);
      imageResizer.execute();
      resizedImage = imageResizer.getResizedImage();
      ImageIO.write(resizedImage, "jpg", new File(filePath + "1.jpg"));

      // key step
      AppboxImg appboxImg = new AppboxImg();
      appboxImg.setImgUrl(url);
      appboxImg.setOriginPath(outputPath + fileNamePrefix + ".jpg");
      appboxImg.setTinyPath(outputPath + fileNamePrefix + "0.jpg");
      appboxImg.setMiddlePath(outputPath + fileNamePrefix + "1.jpg");

      appboxImgDao.save(appboxImg);
    } catch (IOException e) {
      log.error("fetch image file failed");
      return;
    }

    return;
  }