Esempio n. 1
0
  private String uploadImg(UploadFile uploadFile) {
    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");
      throw new ServiceException("file.upload.failed");
    }

    if (uploadFile.validate()) {
      String filePath = "";
      String fileName = uploadFile.getFileName() + "_" + System.currentTimeMillis();
      try {
        uploadFile.upload(ServerConfig.get("real_root_path") + outputPath, fileName);
      } catch (IOException e) {
        log.error("UploadFile upload excute error");
        throw new ServiceException("file.upload.failed");
      }
      filePath = outputPath + fileName;
      return filePath;
    } else {
      log.error("uploadFile validate failed");
      throw new ServiceException("file.upload.failed");
    }
  }
Esempio n. 2
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;
  }