Question subject: Download a PDF file using JSP or servlet
Posted: Thu Aug 11, 2011 8:04 am
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
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.
msi_333
Question subject: Re: Download a PDF file using JSP or servlet
Posted: Thu Aug 11, 2011 4:26 pm
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
cororo
Question subject: Re: Download a PDF file using JSP or servlet
Posted: Thu Aug 11, 2011 8:41 pm
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.
cororo
Question subject: Re: Download a PDF file using JSP or servlet
Posted: Thu Aug 11, 2011 8:54 pm
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 ???
Question subject: Re: Download a PDF file using JSP or servlet
Posted: Fri Aug 12, 2011 3:01 am
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 )
cororo
Question subject: Re: Download a PDF file using JSP or servlet
Posted: Fri Aug 12, 2011 3:16 am
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.
msi_333
Question subject: Re: Download a PDF file using JSP or servlet
Posted: Fri Aug 12, 2011 4:56 pm
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 )