Error in PDF download: Unknown Protocol: c

Asked

Viewed 128 times

0

I am trying to download a PDF file using JSF..

But when executing the method, Exception is launched.

My Method:

public void download(String pathUrl) throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

        response.reset();   // Algum filtro pode ter configurado alguns cabeçalhos no buffer de antemão. Queremos livrar-se deles, senão ele pode colidir.
        response.setHeader("Content-Type", "application/pdf");  // Define apenas o tipo de conteúdo, Utilize se necessário ServletContext#getMimeType() para detecção automática com base em nome de arquivo. 
        OutputStream responseOutputStream = response.getOutputStream();

        // Lê o conteúdo do PDF
        URL url = new URL(pathUrl);
        InputStream pdfInputStream = url.openStream();

        // Lê o conteúdo do PDF e grava para saída
        byte[] bytesBuffer = new byte[2048];
        int bytesRead;
        while ((bytesRead = pdfInputStream.read(bytesBuffer)) > 0) {
            responseOutputStream.write(bytesBuffer, 0, bytesRead);
        }    
        responseOutputStream.flush();

        // Fecha os streams
        pdfInputStream.close();
        responseOutputStream.close();         
        facesContext.responseComplete();         
    }

The Thrown Exception:

java.net.Malformedurlexception: Unknown Protocol: c at java.net.URL. (Unknown Source) at java.net.URL. (Unknown Source) at java.net.URL. (Unknown Source) at br.com.cogerh.template.controller.RelatorioBean.download(Reportbean.java:137) at br.com.cogerh.template.controller.RelatorioBean.gerarCapa(Reportbean.java:74) at br.com.cogerh.template.controller.RelatorioBean.gerarRelatory(Reporteriobean.java:62) at br.com.cogerh.template.controller.RelatorioBean.init(Reportbean.java:49) at sun.reflect..Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect..Nativemethodaccessorimpl.invoke(Unknown Source) at sun.reflect.Delegatingmethodaccessorimpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.Beans.factory.Annotation.Initdestroyannotationbeanpostprocessor$Lifecycleelement.invoke(Initdestroyannotationbeanpostprocessor.java:344) at org.springframework.Beans.factory.Annotation.Initdestroyannotationbeanpostprocessor$Lifecyclemetadata.invokeInitMethods(Initdestroyannotationbeanpostprocessor.java:295) at org.springframework.Beans.factory.Annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(Initdestroyannotationbeanpostprocessor.java:130) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(Abstractautowirecapablebeanfactory.java:396) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(Abstractautowirecapablenfactory.java:1505) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(Abstractautowirecapablebeanfactory.java:521) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(Abstractautowirecapablenfactory.java:458) at org.springframework.Beans.factory.support.Abstractbeanfactory$2.getObject(Abstractbeanfactory.java:334) at br.com.cogerh.template.util.ViewScope.get(Viewscope.java:23) at org.springframework.Beans.factory.support.AbstractBeanFactory.doGetBean(Abstractbeanfactory.java:330) at org.springframework.Beans.factory.support.AbstractBeanFactory.getBean(Abstractbeanfactory.java:194) at org.springframework.context.support.Abstractapplicationcontext.getBean(Abstractapplicationcontext.java:1117) at org.springframework.Beans.factory.access.el.Springbeanelresolver.getValue(Springbeanelresolver.java:56) at com.sun.faces.el.Demuxcompositeelresolver. _getValue(Demuxcompositeelresolver.java:176) at com.sun.faces.el.Demuxcompositeelresolver.getValue(Demuxcompositeelresolver.java:203) at org.apache.el.parser.Astidentifier.getValue(Astidentifier.java:72) at org.apache.el.parser.Astvalue.getValue(Astvalue.java:161) at org.apache.el.ValueExpressionImpl.getValue(Valueexpressionimpl.java:185) at com.sun.faces.facelets.el.TagValueExpression.getValue(Tagvalueexpression.java:109) at javax.faces.Component.ComponentStateHelper.Eval(Componentstatehelper.java:194) at javax.faces.Component.ComponentStateHelper.Eval(Componentstatehelper.java:182) at javax.faces.Component.UIOutput.getValue(Uioutput.java:174) at javax.faces.Component.UIInput.getValue(Uiinput.java:291) at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(Htmlbasicinputrenderer.java:205) at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(Htmlbasicrenderer.java:355) at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(Htmlbasicrenderer.java:164) at javax.faces.Component.UIComponentBase.encodeEnd(Uicomponentbase.java:919) at javax.faces.Component.UIComponent.encodeAll(Uicomponent.java:1864) at javax.faces.render.Renderer.encodeChildren(Renderer.java:176) at javax.faces.Component.UIComponentBase.encodeChildren(Uicomponentbase.java:889) at javax.faces.Component.UIComponent.encodeAll(Uicomponent.java:1857) at javax.faces.Component.UIComponent.encodeAll(Uicomponent.java:1860) at javax.faces.Component.UIComponent.encodeAll(Uicomponent.java:1860) at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(Faceletviewhandlingstrategy.java:461) at com.sun.faces.application.view.MultiViewHandler.renderView(Multiviewhandler.java:133) at javax.faces.application.ViewHandlerWrapper.renderView(Viewhandlerwrapper.java:337) at org.apache.myfaces.tomahawk.application.ResourceViewHandlerWrapper.renderView(Resourceviewhandlerwrapper.java:169) at com.sun.faces.lifecycle.Renderresponsephase.execute(Renderresponsephase.java:120) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.Lifecycleimpl.render(Lifecycleimpl.java:219) at javax.faces.webapp.FacesServlet.service(Facesservlet.java:647) at org.apache.Applicationfilterchain.internalDoFilter(Applicationfilterchain.java:305) at org.apache.Catalina.core.Applicationfilterchain.doFilter(Applicationfilterchain.java:210) at org.apache.Catalina.core.Standardwrappervalve.invoke(Standardwrappervalve.java:222) at org.apache.Catalina.core.Standardcontextvalve.invoke(Standardcontextvalve.java:123) at org.apache.Catalina.authenticator.Authenticatorbase.invoke(Authenticatorbase.java:472) at org.apache.Catalina.core.Standardhostvalve.invoke(Standardhostvalve.java:171) at org.apache.Catalina.valves.Errorreportvalve.invoke(Errorreportvalve.java:99) at org.apache.Catalina.valves.Accesslogvalve.invoke(Accesslogvalve.java:936) at org.apache.Catalina.core.Standardenginevalve.invoke(Standardenginevalve.java:118) at org.apache.Catalina.connector.Coyoteadapter.service(Coyoteadapter.java:407) at org.apache.Coyote.http11.Abstracthttp11processor.process(Abstracthttp11processor.java:1004) at org.apache.Coyote.Abstractprotocol$Abstractconnectionhandler.process(Abstractprotocol.java:589) at org.apache.Tomcat.util.net.Jioendpoint$Socketprocessor.run(Jioendpoint.java:312) at java.util.Concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.Concurrent.Threadpoolexecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

1 answer

1


I was able to solve the problem as follows:

I added the String

file:///

To my pathURL, thus staying the method:

public void download(String pathUrl) throws IOException {
    pathUrl = "file:///"+pathUrl;
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

    response.reset();   // Algum filtro pode ter configurado alguns cabeçalhos no buffer de antemão. Queremos livrar-se deles, senão ele pode colidir.
    response.setHeader("Content-Type", "application/pdf");  // Define apenas o tipo de conteúdo, Utilize se necessário ServletContext#getMimeType() para detecção automática com base em nome de arquivo. 
    OutputStream responseOutputStream = response.getOutputStream();

    // Lê o conteúdo do PDF
    URL url = new URL(pathUrl);
    InputStream pdfInputStream = url.openStream();

    // Lê o conteúdo do PDF e grava para saída
    byte[] bytesBuffer = new byte[2048];
    int bytesRead;
    while ((bytesRead = pdfInputStream.read(bytesBuffer)) > 0) {
        responseOutputStream.write(bytesBuffer, 0, bytesRead);
    }    
    responseOutputStream.flush();

    // Fecha os streams
    pdfInputStream.close();
    responseOutputStream.close();         
    facesContext.responseComplete();         
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.