Thursday 3 October 2013

Glassfish 4 and Primeface 4.0.RC1 fileupload issues

Glassfish 4 and Primeface fileupload issues!

Maven dependencies:


<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>4.0.RC1</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3</version>
</dependency>

uploadFile.getContents() method is not working in primeface 4.0.RC1 version. To get the file content, use the below code:


private byte[] getFileContents(InputStream in) {

byte[] bytes = null;
try {
   // 277*121, 292*170, 327*143, 586*340
   // write the inputStream to a FileOutputStream            
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   int read = 0;
   bytes = new byte[1024];

   while ((read = in.read(bytes)) != -1) {

bos.write(bytes, 0, read);
   }
   bytes = bos.toByteArray();
   in.close();
   in = null;
   bos.flush();
   bos.close();
   bos = null;
   logger.debug("New file created!");
} catch (IOException e) {
   System.out.println(e.getMessage());
}
return bytes;

}

getFileContents(getFile().getInputstream());

1 comment:

  1. Hi, thanks for the solution, I use tomcat7 and primefaces 4.0 and I had the same issue, but now it`s solved.
    Regards, Adrian

    ReplyDelete