FileUpload ViewExpiredException PrimeFaces2.2.1

UI Components for JSF
johnpage09
Posts: 5
Joined: 10 Nov 2011, 08:08

15 Nov 2011, 10:50

Dear All,
I am new PrimeFaces user and have been impressed with the features and the updates. Its a great product and a big thank you to all the contributors.
While most of the features I have implemented are working fine, I am getting an exception when I try to upload a file.
I am using MyFaces 2.0.4, PrimeFaces 2.2.1, Tomcat 7.0.22, Java 1.6, commons-fileupload 1.2.2, commons-io 1.4

The exception is as under.
-----------------------
WARNING: Identifier for execute FlashMap was lost on the postback, thus FlashScope information is gone.
15/11/2011 6:57:52 PM org.apache.myfaces.shared_impl.util.StateUtils reconstruct
SEVERE: View State cannot be reconstructed
javax.faces.FacesException: javax.faces.application.ViewExpiredException
at org.apache.myfaces.shared_impl.util.StateUtils.decrypt(StateUtils.java:496)
at org.apache.myfaces.shared_impl.util.StateUtils.reconstruct(StateUtils.java:378)
at org.apache.myfaces.renderkit.html.HtmlResponseStateManager.getSavedState(HtmlResponseStateManager.java:213)
at org.apache.myfaces.renderkit.html.HtmlResponseStateManager.getState(HtmlResponseStateManager.java:160)
at org.apache.myfaces.view.facelets.DefaultFaceletsStateManagementStrategy.restoreView(DefaultFaceletsStateManag
ementStrategy.java:144)
at org.apache.myfaces.application.jsp.JspStateManagerImpl.restoreView(JspStateManagerImpl.java:388)
at org.apache.myfaces.shared_impl.view.ViewDeclarationLanguageBase.restoreView(ViewDeclarationLanguageBase.java:
106)
at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.restoreView(FaceletViewDeclarationLanguage.ja
va:1438)

---------------------------

I have a Filter Defined in the web.xml
<!-- Filter for File Upload (Prime Faces) -->
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>2097152</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

--------
My upload component has been defined as follows:
<h:form id="uploadForm" enctype="multipart/form-data">
<h:panelGrid columns="1" rendered="#{loginBean.uploadSectionVisible}">
<p:fileUpload fileUploadListener="#{fileUploadBean.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

<p:growl id="messages" showDetail="true"/>
</h:panelGrid>
</h:form>

My handleFileUpload method in the backing bean is defined as follows:
public void handleFileUpload(FileUploadEvent event) {

//Primitives
final int BUFFER_SIZE = 6124;
String folderToUpload;
// FacesContext context = FacesContext.getCurrentInstance();
// UploadedFile file = event.getFile();

System.out.println("File upload triggered!!");

ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();

File result = new File(extContext.getRealPath("//WEB-INF//files//" +
event.getFile().getFileName()));
//System.out.println(extContext.getRealPath("//WEB-INF//files//" +
// event.getFile().getFileName()));

try {
FileOutputStream fileOutputStream = new FileOutputStream(result);

byte[] buffer = new byte[BUFFER_SIZE];

int bulk;
InputStream inputStream = event.getFile().getInputstream();
while (true) {
bulk = inputStream.read(buffer);
if (bulk < 0) {
break;
}
fileOutputStream.write(buffer, 0, bulk);
fileOutputStream.flush();
}

fileOutputStream.close();
inputStream.close();

FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName()
+ " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);

} catch (IOException e) {
e.printStackTrace();

FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"The files were not uploaded!", "");
FacesContext.getCurrentInstance().addMessage(null, error);
}


}

I have tried upgrading to PrimeFaces 3.0.M4 but that broke my Theme layout even though I upgraded to 1.0.2 and so I switched back to 2.2.1

I am not using any other Faces component as PrimeFaces has met all my requirements. If I can't fix this I might have to switch to Tomahawk.
Any help will be much appreciated.

Kind Regards,
John

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

15 Nov 2011, 11:01

We had the same problem - we fixed that wih switching to Mojarra and Server-Side-Viewstate.
But i would use MyFaces and upgrade to 3.0.M4!
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

15 Nov 2011, 11:31

Try new fileupload of 3.0.M4, much better than 2.2.

http://www.primefaces.org/showcase-labs ... adHome.jsf

johnpage09
Posts: 5
Joined: 10 Nov 2011, 08:08

15 Nov 2011, 12:54

optimus.prime wrote:Try new fileupload of 3.0.M4, much better than 2.2.

http://www.primefaces.org/showcase-labs ... adHome.jsf
Thanks Catagay. I did try to upgrade by my view didn't render correctly even though I upgraded my Theme jar. I don't want to move from one problem to another.
Is there a known solution for the problem I am facing. It would be much appreciated.
By the way, I am using Chrome.

Regards,
John

johnpage09
Posts: 5
Joined: 10 Nov 2011, 08:08

15 Nov 2011, 13:10

zoigl wrote:We had the same problem - we fixed that wih switching to Mojarra and Server-Side-Viewstate.
But i would use MyFaces and upgrade to 3.0.M4!
Thanks for the tip zoigl. My ViewExpiredException is now gone after I changed to server side saving so that's a step forward.

johnpage09
Posts: 5
Joined: 10 Nov 2011, 08:08

17 Nov 2011, 08:40

johnpage09 wrote:
zoigl wrote:We had the same problem - we fixed that wih switching to Mojarra and Server-Side-Viewstate.
But i would use MyFaces and upgrade to 3.0.M4!
Thanks for the tip zoigl. My ViewExpiredException is now gone after I changed to server side saving so that's a step forward.
Zoigl. I have upgraded to 3.0.M4 and my view is rendering fine.When I click on the upload button, everything appears to go well
and there are no errors but I can't see the file on the WebServer.
Where should I be looking? I am running on Tomcat7.0.22
and here is the code of my bean method...

//Primitives
final int BUFFER_SIZE = 6124;

public void handleFileUpload(FileUploadEvent event) {
ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
File result = new File(extContext.getRealPath("c:\\temp\\" +
event.getFile().getFileName()));
//System.out.println(extContext.getRealPath("//WEB-INF//files//" +
// event.getFile().getFileName()));

try {
FileOutputStream fileOutputStream = new FileOutputStream(result);

byte[] buffer = new byte[BUFFER_SIZE];

int bulk;
InputStream inputStream = event.getFile().getInputstream();
while (true) {
bulk = inputStream.read(buffer);
if (bulk < 0) {
break;
}
fileOutputStream.write(buffer, 0, bulk);
fileOutputStream.flush();
}

fileOutputStream.close();
inputStream.close();

FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName()
+ " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);

} catch (IOException e) {
e.printStackTrace();

FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"The files were not uploaded!", "");
FacesContext.getCurrentInstance().addMessage(null, error);

}

joseberea
Posts: 4
Joined: 02 Dec 2011, 15:17

02 Dec 2011, 15:20

How can i change it to server side?

Thanks!

joseberea
Posts: 4
Joined: 02 Dec 2011, 15:17

07 Dec 2011, 09:46

Please... can somebody help me?

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

07 Dec 2011, 13:16


joseberea
Posts: 4
Joined: 02 Dec 2011, 15:17

07 Dec 2011, 14:08

kukeltje wrote:sure... http://bit.ly/vHGeB6
I think it's obvious that if I had found the solution there, I would not have asked.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 43 guests