Ejemplo n.º 1
0
  public static List<String> createThumbnails(MultipartFormData body, String folder)
      throws IOException, FileTooLargeException {
    // TODO Auto-generated method stub
    int index = -1;
    List<String> files = new ArrayList<>();
    while (true) {

      FilePart picture = body.getFile("picture" + (index == -1 ? "" : index));
      if (picture != null) {
        String fileName = picture.getFilename();
        File file = picture.getFile();

        System.out.println(file.length());

        BufferedImage image = ImageIO.read(file);

        BufferedImage thumb = ImageUtils.resizeImage(image);

        Logger.error(
            "size: " + file.length() + "   name: " + fileName + "  " + fileName.lastIndexOf('.'));

        fileName = fileName.substring(0, fileName.lastIndexOf('.'));

        Logger.error(
            "size: " + file.length() + "   name: " + fileName + "  " + fileName.lastIndexOf('.'));

        int size = (int) (file.length() / 1024 / 1024);
        if (size > 1) {
          throw new FileTooLargeException(size + " " + 1);
        }

        File fileToSave = generateFileName(fileName, 0, folder);

        fileToSave.createNewFile();
        ImageIO.write(thumb, "png", fileToSave);
        files.add(fileToSave.getPath().toString());
        index++;
      } else break;
    }
    if (files.size() > 0) {
      return files;
    }

    return null;
  }
Ejemplo n.º 2
0
 @Test
 public void rescale() throws IOException {
   ImageUtils imageUtils = new ImageUtils();
   FileInputStream is = new FileInputStream("E:/test/image/108.jpg");
   BufferedImage image = imageUtils.resizeImage(is, 120, 0);
 }