1

The code written below is to download the file from the database by using JSF2.0/Primefaces. The code below which is SessionScoped Bean runs perfectly fine and I am able to download the stored file

@ManagedBean
@SessionScoped
public class FileProcessBB{

private ScreenDisplayData screenDisplayData;

    public void selectAttachment() {
        AttachedFileTable attachedFileTable = new AttachedFileTableManager().getAttachedFileById(screenDisplayData.getSelectedAttachedFileBO().getAttachedFileTableId());
        InputStream inputStream = new ByteArrayInputStream(attachedFileTable.getAttachedFile());
        String contentType = attachedFileTable.getAttachedFileContentType();
        String fileName = attachedFileTable.getAttachedFileName();
        screenDisplayData.setAttachedFileStreamContent(new DefaultStreamedContent(inputStream, contentType, fileName));
    }
}

But I want implement the same functionality into ViewScoped Bean. To achieved that I made necessary changes as below (implementing Serializable for ViewScoped Bean)

@ManagedBean
@ViewScoped
public class FileProcessBB implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 6137719800118206851L;

private ScreenDisplayData screenDisplayData;

    public void selectAttachment() {
        AttachedFileTable attachedFileTable = new AttachedFileTableManager().getAttachedFileById(screenDisplayData.getSelectedAttachedFileBO().getAttachedFileTableId());
        InputStream inputStream = new ByteArrayInputStream(attachedFileTable.getAttachedFile());
        String contentType = attachedFileTable.getAttachedFileContentType();
        String fileName = attachedFileTable.getAttachedFileName();
        screenDisplayData.setAttachedFileStreamContent(new DefaultStreamedContent(inputStream, contentType, fileName));
    }
}

ScreenDisplayData

public class ScreenDisplayData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -3339259618782904262L;

    private StreamedContent attachedFileStreamContent;
    private AttachedFileBO selectedAttachedFileBO;


    //getter setter

}

I have implements Serializable in the AttachedFileBO class also. AttachedFileTable is an Entity class hence it already implements Serializable

Where did I am missing Serializable?

EXCEPTION I am getting is as below

[ERROR   ] SRVE0777E: Exception thrown by application class 'java.io.ObjectOutputStream.writeObject0():1200'
java.io.NotSerializableException: java.io.ByteArrayInputStream
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1200)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1513)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
  at java.util.HashMap.writeObject(HashMap.java:867)
  at sun.reflect.GeneratedMethodAccessor156.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
  at java.lang.reflect.Method.invoke(Method.java:613)
  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1047)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1500)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1382)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1190)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
  at java.util.HashMap.writeObject(HashMap.java:867)
  at sun.reflect.GeneratedMethodAccessor156.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
  at java.lang.reflect.Method.invoke(Method.java:613)
  at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1047)
  at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1500)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1436)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1194)
  at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1382)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1190)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:363)
  at org.apache.myfaces.shared_impl.util.StateUtils.getAsByteArray(StateUtils.java:273)
  at org.apache.myfaces.shared_impl.util.StateUtils.construct(StateUtils.java:235)
  at org.apache.myfaces.renderkit.html.HtmlResponseStateManager.getViewState(HtmlResponseStateManager.java:314)
  at org.apache.myfaces.application.jsp.JspStateManagerImpl.getViewState(JspStateManagerImpl.java:626)
  at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialRendering(PartialViewContextImpl.java:443)
  at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:344)
  at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:88)
  at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:358)
  at javax.faces.component.UIComponent.encodeAll(UIComponent.java:609)
  at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1159)
  at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:263)
  at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
  at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
  at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1221)
  at [internal classes]
SXV
  • 277
  • 5
  • 16
  • `java.io.NotSerializableException: java.io.ByteArrayInputStream` that say it all, you are using `ByteArrayInputStream` inside one of you class and it is not serializable. – Alexandre Lavoie Dec 11 '13 at 11:54
  • I am using ByteArrayInputStream in SessionScoped Bean also but it is working there – SXV Dec 11 '13 at 12:02
  • 1
    this Exception occurs only when stopping the application server. It tries to save all sessions objects to get them back (exemple : restarting server). This wont brake anything in the application functionnality. – Alexandre Lavoie Dec 11 '13 at 12:04

1 Answers1

1

In the model, you should not be using InputStream flavored properties at all. You should be using byte[] instead.

Replace non-serializable InputStream based properties like

private ByteArrayInputStream content;

by serializable byte[] based properties like

private byte[] content;

Transforming between the one and the other form should be done purely inside method scope.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    I am using primefaces DefaultStreamedContent method which takes argument as InputStream. Do you have any suggestion what should I use instead of DefaultStreamedContent – SXV Dec 11 '13 at 13:02
  • 1
    Do not use `private StreamedContent attachedFileStreamContent;`. Use `private byte[] content;` and then create `StreamedContent` in the method which needs to create it based on content. See also among others http://stackoverflow.com/questions/16934663/displaying-images-from-mysql-database-in-jsf-datatable/16940909#16940909 for an elaborate example. – BalusC Dec 11 '13 at 13:10