I am getting the following exception when passing an object from a servlet to Java code
Exception :java.io.StreamCorruptedException: InputStream does not contain a serialized object at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java(Compiled Code)) at java.io.ObjectInputStream.(ObjectInputStream.java(Compiled Code)) at com.tollgate.tree.EtgUploadAction.downloadFile(EtgUploadAction.java:618) at java.lang.reflect.Method.invoke(Native Method) at org.apache.struts.actions.DispatchAction.perform(DispatchAction.java:236) at org.apache.struts.action.ActionServlet.processActionPerform(ActionServlet.java:1787) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1586) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:510) at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code)) at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code)) at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java(Compiled Code)) at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java(Compiled Code)) at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java(Compiled Code)) at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java(Inlined Compiled Code)) at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java(Compiled Code)) at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java(Compiled Code)) at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java(Inlined Compiled Code)) at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java(Compiled Code)) at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java(Compiled Code)) at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java(Compiled Code)) at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java(Compiled Code)) at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java(Compiled Code)) at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java(Compiled Code)) at com.ibm.ws.webcontainer.cache.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java(Compiled Code)) at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java(Compiled Code)) at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java(Compiled Code)) at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java(Compiled Code)) at com.ibm.ws.http.HttpConnection.readAndHandleRequest
Basically I am trying to implement Download File functionality. I have written a java method which passes the file path to the servlet. The servlet then reads the file from the server to a byte array object and sends this object to the java method which inturn displays the contents. The following is the code for your reference:
Java Code -->
public ActionForward downloadFile(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
HttpSession session = request.getSession(true);
String strDebugMsg = "";
try {
strDebugMsg= "In EtguploadAction downloadfile Start";
session.setAttribute("sessStrDebugMsg",strDebugMsg);
ServletOutputStream outStream;
byte[] pic;
ObjectOutputStream out;
ObjectInputStream in;
outStream = response.getOutputStream();
String strServletUrl;
EtgUploadForm objForm = (EtgUploadForm) form;
int intDocId = objForm.getIntDocumentId();
int intPrjId = objForm.getIntProjectId();
int intRevNumber = objForm.getIntRevNumber();
int intTaskId = objForm.getIntTaskId();
String strfileSelected = request.getParameter("strFileName");
String strFileRelativePath;
EtgProps objProp = new EtgProps();
String strBasePath = "";
String strUrl=HttpUtils.getRequestURL(request).toString();
if(strUrl.indexOf("dev")!=-1)
{
strBasePath = objProp.getProperty("strUrlUploadDev");
strServletUrl = objProp.getProperty("strUrlDownloadServletDev");
}
else if(strUrl.indexOf("stage")!=-1)
{
strBasePath = objProp.getProperty("strUrlUploadStage");
strServletUrl = objProp.getProperty("strUrlDownloadServletStage");
}
else
{
strBasePath = objProp.getProperty("strUrlUploadProd");
strServletUrl = objProp.getProperty("strUrlDownloadServletProd");
}
strDebugMsg= strDebugMsg + ";Servletpath" + strServletUrl;
session.setAttribute("sessStrDebugMsg",strDebugMsg);
System.out.println("strServletUrl "+strServletUrl);
String pathAndFileName =
strBasePath
+ "/"
+ intPrjId
+ "/"
+ intTaskId
+ "/OE/"
+ intRevNumber
+ "/"
+ strfileSelected;
strDebugMsg= strDebugMsg + ";Filepath" + pathAndFileName;
session.setAttribute("sessStrDebugMsg",strDebugMsg);
response.setContentType("octetstream/ rubbish");
response.setHeader(
"Content-Disposition",
"attachment;filename=" + strfileSelected + ";");
strDebugMsg= strDebugMsg + ";before submitting to servlet";
session.setAttribute("sessStrDebugMsg",strDebugMsg);
URL url = new URL(strServletUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty ("Content-Type", "application/octet-stream");
conn.setUseCaches (false);
conn.setDefaultUseCaches (false);
out = new ObjectOutputStream( conn.getOutputStream() );
out.writeObject( pathAndFileName );
out.flush();
out.close();
strDebugMsg= strDebugMsg + ";after comin back from servlet b4 getInputStream";
session.setAttribute("sessStrDebugMsg",strDebugMsg);
in = new ObjectInputStream(conn.getInputStream());
try
{
MyByteClass objMyByte = (MyByteClass) in.readObject();
try
{
pic = (byte[]) objMyByte.getByteData();
outStream.write(pic);
outStream.flush();
outStream.close();
}
catch(Exception exp)
{
strDebugMsg= strDebugMsg + ";exception while getByteData()" + exp.getMessage();
session.setAttribute("sessStrDebugMsg",strDebugMsg);
}
}
catch(Exception ex)
{
strDebugMsg= strDebugMsg + ";exception while readObject()" + ex.getMessage();
session.setAttribute("sessStrDebugMsg",strDebugMsg);
}
finally
{
in.close();
outStream.close();
}
strDebugMsg= strDebugMsg + ";In EtguploadAction downloadfile End";
session.setAttribute("sessStrDebugMsg",strDebugMsg);
return (mapping.findForward(""));
} catch (Exception e) {
ByteArrayOutputStream biteStream = new ByteArrayOutputStream(1000);
PrintStream ps = new PrintStream(biteStream, true);
e.printStackTrace(ps);
String strMsg = " Exception :"+biteStream.toString();
ps.close();
ps = null;
biteStream = null;
strDebugMsg= strDebugMsg + ";exception while downloadFile()" + e.getMessage();
session.setAttribute("sessStrDebugMsg",strDebugMsg);
strDebugMsg= strDebugMsg + ";trace while downloadFile()" + strMsg;
session.setAttribute("sessStrDebugMsg",strDebugMsg);
EtgErrors errors = (EtgErrors) request.getAttribute("Errors");
String strError = EtgUtils.logndisplayerror(e);
if (errors == null) {
errors = new EtgErrors();
}
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("program.error", strError),
e,
"EtgUploadAction:downloadFile()");
saveErrors(request, errors);
return (mapping.findForward("failure"));
}
}
Servlet Code -->
public class DownloadServlet extends HttpServlet implements Servlet,Serializable {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try
{
MyByteClass objByte = new MyByteClass();
byte[] pic;
FileInputStream fin = null;
File F = null;
ObjectInputStream in = null;
ObjectOutputStream out = null;
in = new ObjectInputStream( req.getInputStream());
String file = (String)in.readObject();
in.close();
F = new File(file);
if (!F.exists())
throw new FileNotFoundException("The file doesn't exist:"+file);
fin = new FileInputStream(file);
pic = new byte[fin.available()];
fin.read(pic);
objByte.setByteData(pic);
out = new ObjectOutputStream( resp.getOutputStream() );
out.writeObject((MyByteClass)objByte);
out.flush();
out.close();
}
catch(Exception e)
{
EtgErrors errors = (EtgErrors) req.getAttribute("Errors");
String strError = EtgUtils.logndisplayerror(e);
if (errors == null) {
errors = new EtgErrors();
}
errors.add(
ActionErrors.GLOBAL_ERROR,
new ActionError("program.error", strError),
e,
"DownloadServlet:doPost()");
}
}
}
MyByteClass Code -->
public class MyByteClass implements java.io.Serializable {
byte [] byteData =null;
String strData = null;
public void setByteData (byte b[])
{
byteData = b;
}
public byte [] getByteData ()
{
return byteData;
}
public void setStringData (String s)
{
strData = s;
}
public String getStringData ()
{
return strData;
}
}
The java code is throwing an exception at "in = new ObjectInputStream(conn.getInputStream())" as seen from the debug logs. Till then it is working fine. I mean I am able to pass the file path from Java code to Servlet but its giving a problem when passing the byte array object from servlet to Java code. Can you please help.
-
Need help in resolving java.io.StreamCorruptedException: InputSt (4 messages)
- Posted by: Divya Rao
- Posted on: July 09 2004 01:37 EDT
Threaded Messages (4)
- Need help in resolving java.io.StreamCorruptedException: InputSt by Arun Nair on July 09 2004 09:31 EDT
- Need help in resolving java.io.StreamCorruptedException: InputSt by Mohit Gupta on July 09 2004 10:23 EDT
- Need help in resolving java.io.StreamCorruptedException: InputSt by Veera Babu M on July 09 2004 21:15 EDT
- Need help in resolving java.io.StreamCorruptedException: InputSt by Veera Babu M on July 09 2004 21:16 EDT
-
Need help in resolving java.io.StreamCorruptedException: InputSt[ Go to top ]
- Posted by: Arun Nair
- Posted on: July 09 2004 09:31 EDT
- in response to Divya Rao
why can't u use apache commons library - Fileupload. its easy as well..
-arun -
Need help in resolving java.io.StreamCorruptedException: InputSt[ Go to top ]
- Posted by: Mohit Gupta
- Posted on: July 09 2004 10:23 EDT
- in response to Arun Nair
just follow this sample :
client side:
String urlstr = "url of the servlet"
URL servletURL = new URL(urlStr);
HttpURLConnection servletURLConnection
= (HttpURLConnection)servletURL.openConnection();
servletURLConnection.setDoInput(true);
servletURLConnection.setDoOutput(true);
servletURLConnection.setUseCaches (false);
servletURLConnection.setDefaultUseCaches (false);
servletURLConnection.setRequestProperty ("Content-Type",
"application/octet-stream;charset=utf-8");
ObjectOutputStream oosObjectOutputStream = new
ObjectOutputStream(servletURLConnection.getOutputStream());
oosObjectOutputStream.writeObject(sConnMode);
log("Sent String " + sConnMode);
oosObjectOutputStream.writeObject(sOldBlobId);
log("Sent String sOldBlobId " + sOldBlobId);
==============================================================
Servlet Side:
ObjectInputStream oisObjectInputStream = null;
oisObjectInputStream = new ObjectInputStream(req.getInputStream());
String sMode = (String)oisObjectInputStream.readObject();
debug("received string " + sMode);
String sOldBlobId = (String)oisObjectInputStream.readObject();
debug("received string sOldBlobId " + sOldBlobId);
===================================================================
mine is working with this..if u face problem let me know.. -
Need help in resolving java.io.StreamCorruptedException: InputSt[ Go to top ]
- Posted by: Veera Babu M
- Posted on: July 09 2004 21:15 EDT
- in response to Divya Rao
<font face="Comic Sans MS" size="2">Hi Divya,<br>
What you have to do is make sure the file Name which is being sent as an object is existing on th server. That means the file you requested must be there
on the application root directory but not on the application server's root
directory.<br>
<br>
As working with your scenario,<br>
<br>
1) String pathAndFileName = strBasePath+ "/"+ intPrjId+ "/"+ intTaskId+ "/OE/"+intRevNumber+ "/"+ strfileSelected; Must be a valid path<br>
2) throw new FileNotFoundException("The file doesn't exist:"+file); This statement
is showstopper in your application since the requested file is unavailable.<br>
<br>
Let me know incase if it doesn't work still.<br>
<br>
<font color="#FF0000">Note </font> : For you kind information , Do not yo feel this design is complicated. When we have an in-build upload
packages with the framework why should we take risk and make system inconsistent.<br>
</font><br>
-
Need help in resolving java.io.StreamCorruptedException: InputSt[ Go to top ]
- Posted by: Veera Babu M
- Posted on: July 09 2004 21:16 EDT
- in response to Divya Rao
Hi Divya,
What you have to do is make sure the file Name which is being sent as an object is existing on th server. That means the file you requested must be there on the application root directory but not on the application server's root directory.
As working with your scenario,
1) String pathAndFileName = strBasePath+ "/"+ intPrjId+ "/"+ intTaskId+ "/OE/"+intRevNumber+ "/"+ strfileSelected; Must be a valid path
2) throw new FileNotFoundException("The file doesn't exist:"+file); This statement is showstopper in your application since the requested file is unavailable.
Let me know incase if it doesn't work still.
Note : For you kind information , Do not yo feel this design is complicated. When we have an in-build upload packages with the framework why should we take risk and make system inconsistent.