Problema ao fazer um método para download usando Jboss seam

29/09/2009

2

Estava usando o Jboss seam para fazer o download de um arquivo direto do banco de dados

usei o seguinte código na minha view para fazer o upload

<rich:fileUpload fileUploadListener=´#{arquivoHome.upload}´ maxFilesQuantity=´1´ id=´upload´ acceptedTypes=´jpg´ immediateUpload=´true´ >
<a4j:support event=´onuploadcomplete´/>
</rich:fileUpload>

a minha implementação do Listener

public void upload(UploadEvent event){
UploadItem item = event.getUploadItem();
getInstance().setData(item.getData());
getInstance().setContentType(item.getContentType());
getInstance().setDescricao(item.getFileName());
getInstance().setSize(item.getFileSize());
}
Responder

Posts

29/09/2009

Anonymous

Com isso eu consigo salvar tudo no meu banco de dados, abaixo segue a classe que faz o download do arquivo

package org.domain.crudimagem.business;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.domain.crudimagem.entity.Arquivo;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.web.RequestParameter;

@Name(´downloadArquivo´)
public class DownloadArquivo {

@In
private EntityManager entityManager;

@In(value=´#{facesContext.externalContext}´)
private ExternalContext extCtx;

@In(value=´´)
FacesContext facesContext;

@RequestParameter
private Integer arquivoId;

public String download(){

Arquivo arquivo = entityManager.find(Arquivo.class, arquivoId);

HttpServletResponse response = (HttpServletResponse) extCtx.getResponse();

response.setContentType(arquivo.getContentType());

response.addHeader(´Content-disposition´, ´arquivo; filename=\´´ arquivo.getDescricao() ´\´´);

try{
ServletOutputStream os = response.getOutputStream();
os.write(arquivo.getData());
os.flush();
os.close();
facesContext.responseComplete();
}catch(Exception e){
e.printStackTrace();
}

return null;
}
Responder

29/09/2009

Anonymous

Aí eu recebo essa exceção

java.lang.IllegalStateException: Servlet response already use stream, Writer not possible
at org.ajax4jsf.webapp.FilterServletResponseWrapper.getWriter(FilterServletResponseWrapper.java:207)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
at com.sun.facelets.FaceletViewHandler.createResponseWriter(FaceletViewHandler.java:414)
at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:571)
at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)

at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:17
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:15
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:15
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Responder

30/09/2009

Dyego.carmo

Para esta operacao que voce deseja não utilize um BEAN JSF...

Crie uma Servlet direta para fazer este serviço... voce terá todo o controle dela...

este erro ocorre prq vc nao pode simplesmente gravar na saiada... o bean JSF tem um ciclo de vida que nao permite isso.
Responder
×
+1 DevUP
Acesso diário, +1 DevUP
Parabéns, você está investindo na sua carreira