0

Hello fellow developers!

Env: JBoss 7.1, JSF 2.0, Java 6, Richfaces 4.3.3

Problem: UploadedFile item is initialised, item.getName() returns name of uploaded file but item.getData() returns null.

I want to read uploaded file but I'm unable to get to the content.

In variables view of the debugger I can see item.uploadedResource.file = C:\appservers\jboss-as-7.1.1.Final\standalone\tmp\work\jboss.web\default-host\iCargo\richfaces_uploaded_file_8359107924056551868.tmp but there is no file at this location.

web.xml:

<context-param>
  <param-name>createTempFiles</param-name>
  <param-value>true</param-value>
</context-param>

Form:

<h:form enctype="multipart/form-data">
  <rich:fileUpload id="upload" fileUploadListener="#{actionBean.uploadListener}" 
    acceptedTypes="xls,xlsx" immediateUpload="true" maxFilesQuantity="1">
  </rich:fileUpload>
</h:form>

Bean:

private UploadedFile item;
public void uploadListener(FileUploadEvent event) throws Exception {
   item = event.getUploadedFile();
}
Karol
  • 89
  • 13
  • Are you getting any exception on the server? – HashimR Aug 01 '13 at 07:41
  • No. Console is clear but i've just dicovered that file is actually apearing at proper location for a second, and then disapears :) It is visible while I stop at breakpoint on "item = event.getUploadedFile();" line. Do I have to persist this file somehow to have access to it later in session? – Karol Aug 01 '13 at 07:45
  • change `createTempFiles` param value to `false` ... – HashimR Aug 01 '13 at 07:50
  • Caused by: java.io.FileNotFoundException: C:\(..)\richfaces_uploaded_file_8820674655724591088.tmp (Unable to locate file) at java.io.FileInputStream.open(Native Method) [rt.jar:1.6.0_29-ea] - when I try to get to item.getData() in other method. – Karol Aug 01 '13 at 07:55

1 Answers1

0

According to the Richfaces demo fileUpload documentation.:

createTempFiles boolean attribute which defines whether the uploaded files are stored in temporary files or available in listener just as byte[] data (false for this example).

So you need to change createTempFiles param value to false and it will be available to the listener as byte[].

Hope this helps.

EDIT:

Just follow the example in the demo to get the data of the uploaded file.

Community
  • 1
  • 1
HashimR
  • 3,803
  • 8
  • 32
  • 49
  • createTempFiles=false and I get File delete failed: java.io.IOException: File delete failed just after FileInputStream fis = (FileInputStream) event.getUploadedFile().getInputStream(); in uploadListener method :( oh, and I did fallow example thoroughly. – Karol Aug 01 '13 at 08:17
  • OK. Few cleans&restarts, putting bis = new ByteArrayInputStream(event.getUploadedFile().getData()); into listener and miraculously all sprockets begin to work. Thanks for your time HashimR. – Karol Aug 01 '13 at 08:51
  • @HashimR..ho to create TempFiles boolean attribute in web.xml – vijayk Jul 18 '14 at 09:52
  • @Karol..plz help to to solve same problem http://stackoverflow.com/questions/24822211/unable-to-upload-the-file-in-java – vijayk Jul 18 '14 at 09:57