Framework FileUpload da Apache - Dificuldade de capturar Exception via try/catch

4 respostas
A

Como não conseguiu capturar a Exception, mostra o erro abaixo:

SEVERE: org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (2139625) exceeds the configured maximum (2097152)
07/01/2011 08:41:05 org.apache.struts2.interceptor.FileUploadInterceptor intercept
SEVERE: the request was rejected because its size (2139625) exceeds the configured maximum (2097152)

Código completo:

/**
     * Método Action - Responsável por fazer o importe das imagens
     * @return SUCCESS
     * @return INPUT
     * @throws IOException
     * */
	@Override
	public String execute(){
		String formatoImagem1 = imagemDoUsuario1ContentType;
		String formatoImagem2 = imagemDoUsuario2ContentType;
		String formatoImagem3 = imagemDoUsuario3ContentType;	
		for (int i = 1; i < 4; i++) {
			String imagemDoUsuario = "imagemDoUsuario"; 
			String imagem = imagemDoUsuario + i;		
			if(imagem.equals("imagemDoUsuario1")){
				if (imagemDoUsuario1 != null){
			    	if (!formatoImagem1.startsWith("image") && formatoImagem1 == null){
			    		System.out.println("O campo (Foto 1) é nulo");
			    	}else{
			    		if((!TIPOS_ACEITOS.contains(formatoImagem1.substring(6, formatoImagem1.length())))){
			    			throw new IllegalArgumentException("O campo (Foto 1) não recebeu uma foto no formato válido!");
			    		}
					}		    	 
			    }
			}			
			if(imagem.equals("imagemDoUsuario2")){
				if (imagemDoUsuario2 != null){
					if (!formatoImagem2.startsWith("image") && formatoImagem1 == null){
			    		System.out.println("O campo (Foto 2) não recebeu uma foto no formato válido!");
			    	}else{
			    		if((!TIPOS_ACEITOS.contains(formatoImagem2.substring(6, formatoImagem2.length())))){
			    			throw new IllegalArgumentException("O campo (Foto 2) não recebeu uma foto no formato válido!");
			    		}
					}				    	 
			    }
			}			
			if(imagem.equals("imagemDoUsuario3")){
				if (imagemDoUsuario3 != null){
					if (!formatoImagem3.startsWith("image") && formatoImagem1 == null){
			    		System.out.println("O campo (Imagem 3) é nulo!");
			    	}else{
			    		if((!TIPOS_ACEITOS.contains(formatoImagem3.substring(6, formatoImagem3.length())))){
			    			throw new IllegalArgumentException("O campo (Foto 3) não recebeu uma foto no formato válido!");
			    		}
					}			    	 
			    }
			}			
			if (i == 3) {
				if (imagemDoUsuario1 == null && imagemDoUsuario2 == null && imagemDoUsuario3 == null){  
					System.out.println("Não foi recebida nenhuma foto através dos campos do formulário.");
				}
				else{
					//imagem 1
					try{
						if(imagemDoUsuario1.exists()){
						    InputStream entrada1 = null;
							try {
								entrada1 = new FileInputStream(imagemDoUsuario1);
							} catch (FileNotFoundException e) {
								System.err.println("Erro. Foto 1 não foi encontrada!");
								e.printStackTrace();
							}    
						   
						    OutputStream saida1 = new ByteArrayOutputStream();    
							    
						    byte[] buffer1 = new byte[(int)imagemDoUsuario1.length()]; 
							int bytesLidos1 = -1;  
							
							try {
								while ((bytesLidos1 = entrada1.read(buffer1, 0, buffer1.length)) != -1){  
								      saida1.write(buffer1, 0, bytesLidos1);  
								}
							} catch (IOException e) {
								System.err.println("Erro Processando Foto 1!");
								e.printStackTrace();
							}  
							try {
								saida1.flush();								
								saida1.close();
								entrada1.close();
							} catch (IOException e) {
								System.err.println("Erro Processando Foto 1!");
								e.printStackTrace();
							}  
							    
							buffer1 = ((ByteArrayOutputStream) saida1).toByteArray();  
							
							pessoaImagem1.setImagem(buffer1);    
					
							PessoaImagem populaIdImagem = pessoaImagemDAO.salvaImagem(pessoaImagem1, getIdPessoa()); //cria a imagem no banco de dados e, já popula o objeto no populaIdImagem
							idDaImagemPopulado = populaIdImagem.getIdPessoaImagem(); //rebece o id gerado para a 1° imagem recém adiciona
							redimencionamentoDeImagemAction.executaRedimensionamento(buffer1); //passando os Bytes da imagem o idDaImagemPopulado vai ser chamado para servir de parâmetro do nome do thumb
						}
					}catch(SecurityException e){
						System.err.println("O campo (Foto 1) é nulo");
					}catch(NullPointerException e){
						System.err.println("O campo (Foto 1) é nulo");
					}
					try{
						//imagem 2
						if(imagemDoUsuario2.exists()){
						    InputStream entrada2 = null;
							try {
								entrada2 = new FileInputStream(imagemDoUsuario2);
							} catch (FileNotFoundException e) {
								System.err.println("Erro. Foto 2 não foi encontrada!");
								e.printStackTrace();
							}    
						   
						    OutputStream saida2 = new ByteArrayOutputStream();    
							    
						    byte[] buffer2 = new byte[(int)imagemDoUsuario2.length()]; 
							int bytesLidos2 = -1;  
							
							try {
								while ((bytesLidos2 = entrada2.read(buffer2, 0, buffer2.length)) != -1){  
								      saida2.write(buffer2, 0, bytesLidos2);  
								}
							} catch (IOException e) {
								System.err.println("Erro Processando Foto 2!");
								e.printStackTrace();
							}  
							try{
								saida2.flush();  
								saida2.close();  
								entrada2.close();  
							} catch (IOException e) {
								System.err.println("Erro Processando Foto 2!");
								e.printStackTrace();
							} 
							    
							buffer2 = ((ByteArrayOutputStream) saida2).toByteArray();  
							
							pessoaImagem2.setImagem(buffer2);    
								
							PessoaImagem populaIdImagem = pessoaImagemDAO.salvaImagem(pessoaImagem2, getIdPessoa());
							idDaImagemPopulado = populaIdImagem.getIdPessoaImagem();
							redimencionamentoDeImagemAction.executaRedimensionamento(buffer2);
						}
					}catch(SecurityException e){
						System.err.println("O campo (Foto 2) é nulo");
					}catch(NullPointerException e){
						System.err.println("O campo (Foto 2) é nulo");
					}
					try{
						//imagem 3
						if(imagemDoUsuario3.exists()){
						    InputStream entrada3 = null;
							try {
								entrada3 = new FileInputStream(imagemDoUsuario3);
							} catch (FileNotFoundException e) {
								System.err.println("Erro. Foto 3 não foi encontrada!");
								e.printStackTrace();
							}    
						   
						    OutputStream saida3 = new ByteArrayOutputStream();    
							    
						    byte[] buffer3 = new byte[(int)imagemDoUsuario3.length()]; 
							int bytesLidos3 = -1;  
							
							try {
								while ((bytesLidos3 = entrada3.read(buffer3, 0, buffer3.length)) != -1){  
								      saida3.write(buffer3, 0, bytesLidos3);  
								}
							} catch (IOException e) {
								System.err.println("Erro Processando Foto 2!");
								e.printStackTrace();
							}  
							try{
								saida3.flush();  
								saida3.close();  
								entrada3.close();  
							}catch(IOException e){
								e.printStackTrace();
								System.err.println("Erro Processando Foto 2!");
							}
							buffer3 = ((ByteArrayOutputStream) saida3).toByteArray();  
							 
							pessoaImagem3.setImagem(buffer3);    
							
							PessoaImagem populaIdImagem = pessoaImagemDAO.salvaImagem(pessoaImagem3, getIdPessoa());  
							idDaImagemPopulado = populaIdImagem.getIdPessoaImagem();
							redimencionamentoDeImagemAction.executaRedimensionamento(buffer3);
						}
					}catch(SecurityException e){
							System.err.println("O campo (Foto 1) é nulo");
					}catch(NullPointerException e){
						System.err.println("O campo (Foto 3) é nulo");
					}
				}
			}
		}
		return SUCCESS;  	
	}

4 Respostas

A

Detalhe, tirei as tentativas que fiz de tentar capturá-la a exceção SizeLimitExceededException via try/catch.

A

Qualquer sugestão sobre o código será muito bem-vinda.

A

Quem chama esse método é a própria Action criada -> nomeDaAcao.action

A

Any suggestion?

Criado 7 de janeiro de 2011
Ultima resposta 7 de jan. de 2011
Respostas 4
Participantes 1