code: public static void createThumb(
          InputStream input,
          OutputStream output,
          int thumbWidth,
          int thumbHeight,
          String format) throws IOException
      {
          try {
              try {
                  ImageInputStream imageInput = ImageIO
                      .createImageInputStream(input);
                  BufferedImage image = ImageIO.read(imageInput);
                  BufferedImage thumbImage = createThumb(
                      image,
                      thumbWidth,
                      thumbHeight);
                  ImageIO.write(thumbImage, format, output);
              } finally {
                  output.close();
              }
          } finally {
              input.close();
          }
      }
docs: Create a reduced image (thumb) of an image from the given input stream.
  Possible output formats are "jpg" or "png" (no "gif"; it is possible to
  read "gif" files, but not to write). The resulting thumb is written to
  the output stream. The both streams will be explicitly closed by this
  method. This method keeps the ratio width/height of the initial image. If
  both dimensions of the initial image is less than the specified
  boundaries it is not re-scaled; it is written to the output stream as is.
  
  @param input the input stream containing the image to reduce
  @param output the output stream where the resulting reduced image is
  written
  @param thumbWidth the maximal width of the reduced image
  @param thumbHeight the maximal height of the reduced image
  @param format the output format of the reduced image; it can be "jpg",
  "png", ...