File not found exception for temporary files

后端 未结 2 1171
慢半拍i
慢半拍i 2021-01-06 01:22

I\'m building java app (Spring & JSF & PrimeFaces). I upload a file to the server, however, if I click \"next\" button right after the file upload ends, I get this e

相关标签:
2条回答
  • 2021-01-06 01:36

    Without any code, the first thing that comes to mind would be to synchronize threads, so that your button action will wait for the upload process to finish.

    EDIT: From the way you're describing it, your action is apparently being called too soon, so that the file hasn't yet been properly written.

    One way to get around this would be to check if the file exists:

    File file = new File(<path_to_tmp_file>);
      if(file.exists()){
        //Download file
      }
    
    0 讨论(0)
  • 2021-01-06 01:55

    Based on the stack trace, it looks like that you're storing the UploadedFile instance as a property of a class which is by itself Serializable. This is not right. You should be grabbing the uploaded file content immediately in the <p:fileUpload handleFileUpload> file upload listener method (or the submit button in case you are using <p:fileUpload mode="simple">). Store it at a more permanent location. E.g. the local disk file system, or the database, or maybe even as a byte[] bean property. And then pass the local disk file system filename, or database PK, or the byte[] around instead in order to have a handle to download the file back.

    Summarized, just make sure that your Serializable backing bean is completely free of a UploadedFile property and this problem shall disappear.

    0 讨论(0)
提交回复
热议问题