aspose file tools*
The moose likes Servlets and the fly likes problem in getting Blob data and displaying Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem in getting Blob data and displaying" Watch "problem in getting Blob data and displaying" New topic
Author

problem in getting Blob data and displaying

bagvan kat
Greenhorn

Joined: Nov 17, 2003
Posts: 3
Hi, i was able to store a binary data as Blob type in my table, along with name of the file i stired tabel as attachment name, while i want to retrive i am able to get the file name and data from blob but unable to display in the jsp page - (i retrive the data and pass to a bean) - using Struts frame work
My code is below:
package com.actions.event;
import java.io.*;
import java.util.Locale;
import java.util.*;
import java.lang.*;
import java.sql.*;
import java.sql.Statement;
import javax.sql.DataSource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;
import org.apache.struts.util.MessageResources;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.BasicDynaBean;
import com.utils.DBUtil;
public final class ViewAttachment extends Action {
private Log log = LogFactory.getLog("com.actions.event.ViewAttachment");
String userid = null, usertype = null;
String varname = null;

public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {

ActionErrors errors = new ActionErrors();
Locale locale = getLocale(request);
HttpSession session = request.getSession(false);

Connection con = null;
PreparedStatement pStmt = null;
ResultSet rs1 = null;
String attachfilename= request.getParameter("Attachments");
System.out.println("Attachment data is " + attachfilename);

try {

String qry="select Attachments , AttachmentsData from events where Attachments=?";
System.out.println("Qry is " + qry);
con = DBUtil.getConnection();
pStmt = con.prepareStatement(qry);
pStmt.setString(1,attachfilename);
rs1 = pStmt.executeQuery();
if( rs1.next())
{
InputStream is=rs1.getBinaryStream("AttachmentsData");
/*FileOutputStream fos=new FileOutputStream("outfile");
byte [] buff= new byte[8192];
int len;
while(0<(len=is.read(buff)) )
fos.write(buff,0,len);
fos.close();*/
is.close();
System.out.println("Writing Over");
pStmt.close();
con.close();
}else
{
System.out.println("Data Not Available");
}
// request.setAttribute("linkfile",attachfilename);
}catch(Exception ex) {
ex.printStackTrace();
}
System.out.println("Process is over");
return mapping.findForward("success");

}
}
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Did you want to give us a hint why it's failing?
Try getting rid of the try/catch block. You're catching exceptions and printing the stack trace to System.out and pretending that you've handled it. This is a dangerous practice since you lose exceptions.
Dave
bagvan kat
Greenhorn

Joined: Nov 17, 2003
Posts: 3
Hi david,
Thanks for ur info...we tried it but it is giving the classcast exception..which we experienced the same before..i am really looking forwad on ur reply
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
bagvan -
classcastexception, that's a good starting point. Now, can you copy and paste your stack trace so we all can have a look at it. It might help us a little bit


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
bagvan kat
Greenhorn

Joined: Nov 17, 2003
Posts: 3
hi andy,
here is the exception code:
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: [B
at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:457)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:264)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1289)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:484)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:536)

root cause
java.lang.ClassCastException: [B
at com.codestudio.sql.PoolManResultSet.getBinaryStream(Unknown Source)
at com.actions.event.ViewAttachment.execute(ViewAttachment.java:52)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:455)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:264)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1289)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:484)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:536)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in getting Blob data and displaying