Total members 10262 | Gratitudes |It is currently Thu May 24, 2012 10:13 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 7 posts ] 
Author Question
 Question subject: Download a PDF file using JSP or servlet
PostPosted: Thu Aug 11, 2011 8:04 am 
Offline
Newbie
User avatar

Joined: Thu Aug 11, 2011 7:48 am
Posts: 4
Location: Chihuahua/Mexico
Has thanked: 1 time
Have thanks: 0 time

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.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Download a PDF file using JSP or servlet
PostPosted: Thu Aug 11, 2011 4:26 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
are you sure that response reference initialized ?

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )



For this message the author msi_333 has received gratitude : cororo
TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Download a PDF file using JSP or servlet
PostPosted: Thu Aug 11, 2011 8:41 pm 
Offline
Newbie
User avatar

Joined: Thu Aug 11, 2011 7:48 am
Posts: 4
Location: Chihuahua/Mexico
Has thanked: 1 time
Have thanks: 0 time
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.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Download a PDF file using JSP or servlet
PostPosted: Thu Aug 11, 2011 8:54 pm 
Offline
Newbie
User avatar

Joined: Thu Aug 11, 2011 7:48 am
Posts: 4
Location: Chihuahua/Mexico
Has thanked: 1 time
Have thanks: 0 time
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.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Download a PDF file using JSP or servlet
PostPosted: Fri Aug 12, 2011 3:01 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
yes here still equal null .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Download a PDF file using JSP or servlet
PostPosted: Fri Aug 12, 2011 3:16 am 
Offline
Newbie
User avatar

Joined: Thu Aug 11, 2011 7:48 am
Posts: 4
Location: Chihuahua/Mexico
Has thanked: 1 time
Have thanks: 0 time
Hi;
do yoiu know why could be happed ??

_________________
Mike V
Thanks in advance.


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Download a PDF file using JSP or servlet
PostPosted: Fri Aug 12, 2011 4:56 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
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 .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 7 posts ] 
Quick reply


  

 Similar topics
 Connect to database Microsoft access within jsp servlet
 Image Insert in Excel File Using POI
 ASP file structure
 upload and download images in my jsp page.
 Search records from text file
 error closing file matlab save
 create download link
 How to download any file in jsp.
 How can i redirect to a one servlet from a two jsp page?
 read php file in asp and display it as html

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






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