Problema com FileUpload do richfaces

1 resposta
L

Eae pessoal, blz?

estou com um problema no meu jsp, onde tenho um rich:fileUpload e no método eu tenho que armazenar em um array de FILE, quando eu adicionona lista pela segunda vez ele ao invés de adicionar continuamente, não ele zera a lista toda vez que chamo um upload.

o método seria o uploadDocumentos que está falhando.

segue abaixo meu código:

public class Sugestao extends AbstractMB{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<SugestaoTO> sugestoes;
	private SugestaoTO sugestaoTO;
	private String noCCaUl;
	
	SugestaoBean sugestaoBean;
	DocumentoTO documentoTO;
	List<DocumentoTO> documentos;
	
	private Integer quantidadeUploads;
	private Integer quantidadeUploadsDoc;
	private FileByte fileByte;
	private ReaderArquivos reader;
	private File file;
	private List<File> filesDoc = new ArrayList<File>();
	

	public Sugestao() {
		sugestaoBean = new SugestaoBeanImp();
		sugestaoTO = new SugestaoTO();
		documentoTO = new DocumentoTO();
		quantidadeUploadsDoc = 10;
		quantidadeUploads  = 1;
		setCcaUl();
		listar();
	}
	
	/**
	 * @return the sugestoes
	 */
	public List<SugestaoTO> getSugestoes() {
		return sugestoes;
	}
	
	/**
	 * @param sugestoes the sugestoes to set
	 */
	public void setSugestoes(List<SugestaoTO> sugestoes) {
		this.sugestoes = sugestoes;
	}
	
	/**
	 * @return the sugestaoTO
	 */
	public SugestaoTO getSugestaoTO() {
		return sugestaoTO;
	}
	
	/**
	 * @param sugestaoTO the sugestaoTO to set
	 */
	public void setSugestaoTO(SugestaoTO sugestaoTO) {
		this.sugestaoTO = sugestaoTO;
	}
	
	/**
	 * @return the documentoTO
	 */
	public DocumentoTO getDocumentoTO() {
		return documentoTO;
	}
	
	/**
	 * @param documentoTO the documentoTO to set
	 */
	public void setDocumentoTO(DocumentoTO documentoTO) {
		this.documentoTO = documentoTO;
	}
	
	/**
	 * @return the noCCaUl
	 */
	public String getNoCCaUl() {
		return noCCaUl;
	}
	
	/**
	 * @param noCCaUl the noCCaUl to set
	 */
	public void setNoCCaUl(String noCCaUl) {
		this.noCCaUl = noCCaUl;
	}
	
	/**
	 * @return the quantidadeUploads
	 */
	public Integer getQuantidadeUploads() {
		return quantidadeUploads;
	}
	
	/**
	 * @param quantidadeUploads the quantidadeUploads to set
	 */
	public void setQuantidadeUploads(Integer quantidadeUploads) {
		this.quantidadeUploads = quantidadeUploads;
	}

	/**
	 * @return the quantidadeUploadsDoc
	 */
	public Integer getQuantidadeUploadsDoc() {
		return quantidadeUploadsDoc;
	}


	/**
	 * @param quantidadeUploadsDoc the quantidadeUploadsDoc to set
	 */
	public void setQuantidadeUploadsDoc(Integer quantidadeUploadsDoc) {
		this.quantidadeUploadsDoc = quantidadeUploadsDoc;
	}
	
	/**
	 * @return the documentos
	 */
	public List<DocumentoTO> getDocumentos() {
		return documentos;
	}
	
	
	/**
	 * @param documentos the documentos to set
	 */
	public void setDocumentos(List<DocumentoTO> documentos) {
		this.documentos = documentos;
	}
	
	/**
	 * @return the file
	 */
	public File getFile() {
		return file;
	}

	/**
	 * @param file the file to set
	 */
	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * @return the filesDoc
	 */
	public List<File> getFilesDoc() {
		return filesDoc;
	}

	/**
	 * @param filesDoc the filesDoc to set
	 */
	public void setFilesDoc(List<File> filesDoc) {
		this.filesDoc = filesDoc;
	}

	public void listar(){
		try{
			ControlePaginacao pag = getPaginacao(sugestaoTO);
			setSugestoes(sugestaoBean.list(sugestaoTO));
			atualizaValoresDaPaginacao(sugestaoTO, sugestaoBean.count(sugestaoTO), pag);
		}catch(IOException e){
			setMensagem(e);
		}
	}
	

	private void setCcaUl(){
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getRequestContextPath();
		String url = null;
		if (fc.getExternalContext().getRequest() instanceof RenderRequestImpl) {
			url = ((RenderRequestImpl) fc.getExternalContext().getRequest()).getRealRequest().getPathInfo();
		} else if (fc.getExternalContext().getRequest() instanceof ActionRequestImpl){
			url = ((ActionRequestImpl) fc.getExternalContext().getRequest()).getRealRequest().getPathInfo();
		}else{
			url = ((ResourceRequestImpl) fc.getExternalContext().getRequest()).getRealRequest().getPathInfo();
		}
		if(url.contains("siippUL")){
			sugestaoTO.setIcCcaUl(1);
			noCCaUl = "Unidade Lotérica";						
		}else{
			sugestaoTO.setIcCcaUl(2);
			noCCaUl = "Correspondente Caixa Aqui";
		}			
	}		
	
	public void uploadImagem(UploadEvent ev) throws IOException{		
		UploadItem item = ev.getUploadItem();
		fileByte = new FileByte();
		fileByte.setDados(item.getData());
		fileByte.setPath(item.getFileName());
		fileByte.setSize(item.getData().length);
		setFile(fileByte.setBytesFromFile(fileByte.getDados()));	
	}
	
	public String setaMaxUpload(){
		sugestaoTO.setImAnexo(null);
		quantidadeUploads = 1;
		return null;
	}
	
	public void uploadDocumentos(UploadEvent evento) throws IOException{
		
		List<UploadItem> itens = evento.getUploadItems();
		fileByte = new FileByte();
		for (UploadItem item : itens) {
			fileByte.setDados(item.getData());
			fileByte.setPath(item.getFileName());
			fileByte.setSize(item.getData().length);
		
			file = fileByte.setBytesFromFile(fileByte.getDados());
			
			if(fileByte.getPath().contains(".txt")){
				reader = new ReaderArquivos();
				reader.readTxt(fileByte);
			}
						
			filesDoc.add(file);
		}			
	}
<?xml version="1.0" encoding="ISO-8859-1"?>
<div xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:rich="http://richfaces.org/rich"
	xmlns:a4j="http://richfaces.org/a4j">
<f:view>
	<link href="/siipp/stylesheet/dica.css" rel="stylesheet" type="text/css" />
		
		<a4j:form id="form_dicaCCA">
			<f:loadBundle basename="mensagem" var="msg"/>
			
			<h:panelGroup id="contentDica" styleClass="painelPrincipalDica" layout="block">
				<h:panelGroup styleClass="titulo_dica" layout="block">
					<h:outputText styleClass="titulo_suporte_main" value="SUPORTE" />
					<h:graphicImage styleClass="img_seta_tituloMain" value="/imagens/seta_FaqTitulo.png" />
					<h:outputText styleClass="titulo_dica_main" value="DICAS" />
				</h:panelGroup>
				
				<h:panelGroup layout="block" styleClass="background_tituloDicas_inclusao">
					<h:panelGroup layout="block" style="float: left; width: 100%; margin-left: 21px; margin-top: 22px;">
						<h:outputText styleClass="estilo_titulo_Inclusao" value="INCLUIR DICA" />
					</h:panelGroup>
				</h:panelGroup>
				
				<h:panelGroup layout="block" styleClass="background_conteudoDicas_inclusao">
					
					<h:panelGroup layout="block" style="float:left;width:100%;padding-top:10px;padding-left:20px;;">
						<h:outputText value="Dica" style="font-family:Arial; font-size:12pt;color:#666666;
						font-weight:bold;"/>
					</h:panelGroup>	
					
					<h:panelGroup layout="block" style="margin-left: 20px; margin-top: 20px; float: left;">
						<h:outputText style="color:#F60;font-size:7pt;margin-top:-2px;" value="* Campos de preenchimento obrigatório" />
					</h:panelGroup>
					
					<h:panelGroup layout="block" style="margin-left: 20px; margin-top: 10px; float: left;width:100%;">
						<h:panelGroup layout="block" style="margin-bottom: 5px;">
							<h:outputText style="color:#F60;font-size:7pt;margin-right:3px;" value="*" /> 
							<h:outputText style="color:#666666;font-family:Arial,Helvetica,sans-serif;font-size: 9pt;" value="Título da Dica:" />
						</h:panelGroup>
						<h:inputText id="noSugestao" size="40" value="#{dicaMB.sugestaoTO.noSugestao}"/>
					</h:panelGroup>				
					
					<h:panelGroup layout="block" style="margin-left: 20px; margin-top: 10px; float: left;width:100%;">
						<h:panelGroup layout="block" style="margin-bottom: 5px;">
							<h:outputText style="color:#F60;font-size:7pt;margin-right:3px;" value="*" /> 
							<h:outputText style="color:#666666;font-family:Arial,Helvetica,sans-serif;font-size: 9pt;" value="Descrição da Dica:" />
						</h:panelGroup>
						<h:inputText id="deDica" size="40" value="#{dicaMB.sugestaoTO.deSugestao}"/>
					</h:panelGroup>
					
					<h:panelGroup layout="block" style="margin-left: 20px; margin-top: 10px; float: left;width:100%;">
						<h:panelGroup layout="block" style="margin-bottom: 5px;">
							<h:outputText style="color:#F60;font-size:7pt;margin-right:3px;" value="*" /> 
							<h:outputText style="color:#666666;font-family:Arial,Helvetica,sans-serif;font-size: 9pt;" value="Nome do Link:" />
						</h:panelGroup>
						<h:inputText id="edLink" size="40" value="#{dicaMB.sugestaoTO.edLink}"/>
					</h:panelGroup>
					
					<h:panelGroup layout="block" style="margin-left: 20px; margin-top: 10px; float: left;width:100%;">
						<h:panelGroup layout="block" style="margin-bottom: 5px;">
							<h:outputText style="color:#F60;font-size:7pt;margin-right:3px;" value="*" /> 
							<h:outputText style="color:#666666;font-family:Arial,Helvetica,sans-serif;font-size: 9pt;" value="Imagem da Dica:" />
						</h:panelGroup>
							<rich:fileUpload listHeight="58" id="upload"
									acceptedTypes="gif, jpeg, png, jpg"
									maxFilesQuantity="#{dicaMB.quantidadeUploads}" 
									fileUploadListener="#{dicaMB.uploadImagem}"
									cancelEntryControlLabel="Cancelar" 
									clearAllControlLabel="Limpar Tudo" 
									uploadControlLabel="Carregar" 
									addControlLabel="Adicionar" 
									progressLabel="Carregando.."
									clearControlLabel="Limpar"
									stopControlLabel="Parar"
									stopEntryControlLabel="Parar"
									doneLabel="Pronto">
							</rich:fileUpload>
					</h:panelGroup>
					
					<h:panelGroup id="docCampos" style="float:left;width:100%;margin-left:20px;margin-top:25px;" layout="block" >
							
								<h:panelGroup style="float:left;width:100%;margin-bottom:20px;" layout="block">
									<h:outputText value="Documentos da Dica" style="font-family:Arial; font-size:12pt;color:#666666;
								font-weight:bold;"/>
								</h:panelGroup>	
								
								<h:panelGroup style="float:left;width:100%;margin-bottom:10px;" layout="block">
									<h:panelGroup layout="block" style="margin-bottom: 5px;">
										<h:outputText style="color:#F60;font-size:7pt;margin-right:3px;" value="*" /> 
										<h:outputText style="color:#666666;font-family:Arial,Helvetica,sans-serif;font-size: 9pt;" value="Descrição da Documentação:" />
									</h:panelGroup>
									<h:inputText id="deDocumentacao" size="40" value="#{dicaMB.documentoTO.deDocumentacao}"/>
								</h:panelGroup>
													
								<h:panelGroup style="float:left;width:100%;margin-bottom:15px;" layout="block">
									<h:panelGroup layout="block" style="margin-bottom: 5px;">
										<h:outputText style="color:#F60;font-size:7pt;margin-right:3px;" value="*" /> 
										<h:outputText style="color:#666666;font-family:Arial,Helvetica,sans-serif;font-size: 9pt;" value="Documentos:" />
									</h:panelGroup>
									<rich:fileUpload listHeight="116" id="uploadDocumentos" 
										acceptedTypes="pdf, xls, txt, csv, doc" 
										maxFilesQuantity="#{dicaMB.quantidadeUploadsDoc}"
										fileUploadListener="#{dicaMB.uploadDocumentos}"
										cancelEntryControlLabel="Cancelar" 
										clearAllControlLabel="Limpar Tudo" 
										uploadControlLabel="Carregar" 
										addControlLabel="Adicionar" 
										progressLabel="Carregando.."
										clearControlLabel="Limpar"
										stopControlLabel="Parar"
										stopEntryControlLabel="Parar"
										doneLabel="Pronto">
									</rich:fileUpload>				
								</h:panelGroup>
							</h:panelGroup>
						
						<h:panelGroup layout="block" style="float:left;width:100%;padding-left: 17px;">	
									<a4j:commandLink action="#{dicaMB.incluirDica}" style="float:left;text-align: center;padding-right:4px;">
										<h:graphicImage url="/imagens/btn_incluir.png" />
									</a4j:commandLink>
									<a4j:commandLink style="float:left;text-align: center;padding-right:4px;padding-top:8px;">
										<h:graphicImage url="/imagens/btn_cancelar.png" />
									</a4j:commandLink>
						</h:panelGroup>
					</h:panelGroup>
				</h:panelGroup>
		</a4j:form>
	</f:view>
</div>

obrigado.

1 Resposta

thiago.correa

Se eu não estou enganado esse é o comportamento padrão do fileUpload, adicione o atributo autoclear=“false” nele e teste novamente

Criado 30 de maio de 2012
Ultima resposta 30 de mai. de 2012
Respostas 1
Participantes 2