Redimensionar imagem

Olá,

Estou tentando fazer o upload de imagem e gerar dois tamanhos diferentes, mas não esta dando certo.

O primeiro redimensionamento esta funcionando, o problema é quando chamo o segundo redimensionamento.

Alguém poderia me ajudar


 public void uploadImage(WebAddress webAddress, UploadedFile logo) {
			System.out.println("111111111");
			String file = uploadImageSizeTeste(webAddress, logo, imagesPathLarge, MAX_HEIGHT_LARGE, MAX_WIDTH_LARGE);
			System.out.println("22222222");
			String file2 = uploadImageSizeTeste(webAddress, logo, imagesPathSmall, MAX_HEIGHT_SMALL, MAX_WIDTH_SMALL);
			System.out.println("3333333333");
			System.out.println("file2: "+file2);
			
			.....
                        .............


			result.nothing();		
		}



 public String uploadImageSizeTeste(WebAddress webAddress, UploadedFile logo, String path, int H, int W) {
			
			String fileImage = webAddress.getType().getImageLogo() + "_" + webAddress.getId() + ".png";
			
			if (logo != null) {
				
				try {

					/*1-------------------------------------------------------------*/
					
					//String filePath = imagesPath + "/logo_url.png";
					String filePath = path + "/" + fileImage;

					File file = new File(filePath);
					//file.deleteOnExit();
					if (!file.exists()) file.createNewFile();
					 
					Image image = ImageIO.read(logo.getFile());								
					int width = image.getWidth(null); ////////////da java.lang.NullPointerException, aqui na hora que fazer o 2 redimensionamento ////////////////////////////////////////////////
					int height = image.getHeight(null);
					
					if (height > H) {
						width = H * width / height;
						height = H;
						image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);	
					}
					
					
					if (width > W) {
						height = W * height / width;
						width = W;
						image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);	
					}
					
					BufferedImage bufferedImage = new BufferedImage(width, height, 
							BufferedImage.TYPE_INT_ARGB);
					
					
					Graphics2D g2 = bufferedImage.createGraphics();
					g2.drawImage(image, null, null);
					
									
					ImageIO.write(bufferedImage, "png" , file);

				} catch (FileNotFoundException e) {
					e.printStackTrace();
					System.out.println("erro............1");
					//throw new FileNotFoundException("Arquivo não encontrado!");
				} catch (IOException e) {
					e.printStackTrace();
					System.out.println("erro............2");
					//throw new IOException("Não foi possível enviar o arquivo!");
				}
			}
			
			return fileImage;
		}

o UploadedFile te dá um InputStream, e InputStreams só podem ser lidos uma vez… vc precisa transformar isso num byte[] pra poder usar duas vezes.

e como faço para fazer isso?

algo como:

byte[] imagem = ByteStreams.toByteArray(stream);
//ou
byte[] imagem = IOUtils.toByteArray(stream);

Olá,

Não consegui entender onde colocar byte[] imagem = ByteStreams.toByteArray(stream); no método uploadImageSizeTeste.

Poderia me orientar.

Obrigado.

ao invés de fazer:

public String uploadImageSizeTeste(WebAddress webAddress, UploadedFile logo, ..) {
 //...
 ... = ImageIO.read(logo.getFile());
 //...
}

faça:

public String uploadImageSizeTeste(WebAddress webAddress, byte[] imagem, ..) {
 //...
 ... = ImageIO.read(imagem);
 //ou
 ... = ImageIO.read(new ByteArrayInputStream(imagem));
 //...
}