Total members 11343 |It is currently Wed Jul 23, 2014 4:29 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

All times are UTC [ DST ]



  Download a PDF file using JSP or servlet
Thu Aug 11, 2011 8:04 am 

Hi there;
I am using a code I just Have seen here, the code is the next

Code:
// you  can get your base and parent from the database
        String base = "e1";
        String parent = "e2";
        String filename = "Inventario de la linea 402.pdf";    //my own filename
// you can  write http://localhost
        String filepath = "C:/";                                //located on Local C harddisk

        BufferedInputStream buf = null;
        ServletOutputStream myOut = null;

        try {

            myOut = response.getOutputStream();             //Getting mistake here
            File myfile = new File(filepath + filename);

            //set response headers
            response.setContentType("application/pdf");         //I want to download a PDF file

            response.addHeader(
                    "Content-Disposition", "attachment; filename=" + filename);

            response.setContentLength((int) myfile.length());

            FileInputStream input = new FileInputStream(myfile);
            buf = new BufferedInputStream(input);
            int readBytes = 0;

            //read from the file; write to the ServletOutputStream
            while ((readBytes = buf.read()) != -1) {
                myOut.write(readBytes);
            }

        } catch (IOException ioe) {
        } finally {

            //close the input/output streams
            if (myOut != null) {
                myOut.close();
            }
            if (buf != null) {
                buf.close();
            }

        }
    }


the mistake is in the line

Code:
myOut = response.getOutputStream();             //Getting mistake here


11/08/2011 12:54:02 AM javax.faces.event.MethodExpressionActionListener processAction
GRAVE: Se ha recibido 'java.lang.NullPointerException' al invocar la escucha de acción '#{enlineatabla.sapitof}' para el componente 'guardarpdff'
11/08/2011 12:54:02 AM javax.faces.event.MethodExpressionActionListener processAction
GRAVE: java.lang.NullPointerException
at enlineatabla.sapitof(enlineatabla.java:367)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Thread.java:619)

11/08/2011 12:54:02 AM com.sun.faces.context.AjaxExceptionHandlerImpl log
GRAVE: JSF1073: se ha interceptado javax.faces.event.AbortProcessingException durante el procesamiento de INVOKE_APPLICATION 5 : UIComponent-ClientId=j_idt39:guardarpdff, Mensaje=/index.xhtml @135,124 actionListener="#{enlineatabla.sapitof}": java.lang.NullPointerException
11/08/2011 12:54:02 AM com.sun.faces.context.AjaxExceptionHandlerImpl log
GRAVE: /index.xhtml @135,124 actionListener="#{enlineatabla.sapitof}": java.lang.NullPointerException
javax.faces.event.AbortProcessingException: /index.xhtml @135,124 actionListener="#{enlineatabla.sapitof}": java.lang.NullPointerException
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at enlineatabla.sapitof(enlineatabla.java:367)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
... 21 more


this means error on the line like:

Code:
java.lang.NullPointerException
        at enlineatabla.sapitof(enlineatabla.java:367)
        the line is:
        myOut = response.getOutputStream();             //Getting mistake here


Could someone help me ??

thanks in advance
Mike V.

_________________
Mike V
Thanks in advance.


Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
  Re: Download a PDF file using JSP or servlet
Thu Aug 11, 2011 4:26 pm 
are you sure that response reference initialized ?

_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2694
Have thanks: 73 time

For this message the author msi_333 has received gratitude : cororo
  Re: Download a PDF file using JSP or servlet
Thu Aug 11, 2011 8:41 pm 
hi;
Yes in this way:

Code:

    String linea[] = {"Seleccione...", "401", "402", "403", "405", "406", "407", "408",
        "409", "410", "411", "412", "Jaula 1", "Jaula 2", "Jaula 3", "Jaula 4"};
    String estacion[] = {"Seleccione...", "FVT inicial", "FVT final", "Troubleshoot",
        "FA", "Burning Inicial", "Burning Extendido", "Contencion 3-4", "OBE", "Jaula 1",
        "Jaula 2", "Jaula 3", "Jaula 4"};
    String estacion1, linea1;
    String Linea2, estacion2, elemento, tecnico, capitaldtv, estado, fecha;
    Connection conexion;    //mysql
    Statement statement;    //mysql
    ResultSet rs;           //mysql
    List<auxenlineatabla> lista = new ArrayList<auxenlineatabla>();
    Document documento;                                                             //Creado para itext pdf
    PdfPTable tabla;                                                                //Creado para itext pdf
    float[] medidaceldas = {2.90f, 2.90f, 3.10f, 2.90f, 3.50f, 2.90f, 2.90f};       //Creado para itext pdf
    Paragraph pg;                                                                   //Creado para itext pdf
    PdfPCell celda;                                                                 //Creado para itext pdf
    BaseColor color = new BaseColor(192, 192, 192);                                 //Creado para itext pdf   
    String archivo;
    Resource pdfResource;                                                           //Creado para Descargar el pdf
    private static final int BYTES_DOWNLOAD = 1024;
    private Resource pdfResourceDynFileName;
    [color=#BF0040][b]HttpServletResponse response;[/b][/color]
    private String filename;



Then I am using on:

Code:
myOut = response.getOutputStream();             //Getting mistake here
response.setContentType("application/pdf");         //I want to download a PDF file
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
response.setContentLength((int) myfile.length());


I have gotten a dir¡ferents codes and always get same mistake, same line, Do you know waht could be happen??

Mike V.
thanks in advance.


Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
  Re: Download a PDF file using JSP or servlet
Thu Aug 11, 2011 8:54 pm 
Hi;
Yes I initialized the response on this way:

well not initialized but declared, initialized=null ???
Code:
HttpServletResponse response;
HttpServletResponse response=null;


initialized like null, same mistake :tongue: jojo

_________________
Mike V
Thanks in advance.


Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
  Re: Download a PDF file using JSP or servlet
Fri Aug 12, 2011 3:01 am 
yes here still equal null .

_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2694
Have thanks: 73 time
  Re: Download a PDF file using JSP or servlet
Fri Aug 12, 2011 3:16 am 
Hi;
do yoiu know why could be happed ??

_________________
Mike V
Thanks in advance.


Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
  Re: Download a PDF file using JSP or servlet
Fri Aug 12, 2011 4:56 pm 
the question is where you are using this code , if you are using JSP ,there is a response object by default , and if you are using servlet , it should be passed as parameter at GET and POST functions .

_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2694
Have thanks: 73 time
Post new topic Reply to topic  [ 7 posts ] 

  Related Posts  to : Download a PDF file using JSP or servlet
 Link to download in my website..jsp or jsf to download file     -  
 How to download any file in jsp.     -  
 JSP to Download File     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 create download link     -  
 download ftp client softwares     -  
 Java download error     -  
 upload and download images in my jsp page.     -  
 How to download and save .mov files from this website ?     -  
 passguide vcp-410 questions dumps download links     -  

All times are UTC [ DST ]









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team