0

I am making a simple JSF app for my final exam. It's basically one page app with picture and some basic data upload (text, etc). The problem emerges with the picture upload part. I use Primefaces Single File Upload:

    <h:form>
...

<p:fileUpload id="slike" fileUploadListener="#{oglasBean.handleFileUpload}" cancelLabel="Otkaži" label="Dodaj" 
mode="advanced" dragDropSupport="false" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" update="poruka"/>
<div/>
    <br />
    <p:commandButton value="Dodaj oglas"
        action="#{oglasBean.dodajOglas()}" />
...
    </h:form>

The backing bean code:

public void handleFileUpload(FileUploadEvent event) {   
    System.out.println(event.getFile().getFileName());
            oglas.setSlika(event.getFile());            
}

public void dodajOglas(){

    Date datum = new Date();
    oglas.setDatumKreiranja(datum);
    if(oglas.getVrsta().equals("besplatan")){
        oglas.setTrajanje(1);
        oglas.setCijena(0);
    }
    else{

    }

    if (OglasDAO.dodajOglas(oglas)) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,"Uspješno ste dodali sliku.", "");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
    else{
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Dodavanje slike nije uspjelo!", "");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}

The DAO method for adding to the database, actually where the problem shows up:

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

import net.etfbl.traveladvertiser.model.Oglas;

public class OglasDAO {
private static String DODAJ_OGLAS = "insert into oglas values (tekst, slika, 
vrsta, trajanje) values (?, ?, ?, ?)";

public static boolean dodajOglas(Oglas oglas){
    Connection conn = null;
    try{
        conn = ConnectionPool.getConnectionPool().checkOut();
        PreparedStatement stm = conn.prepareStatement(DODAJ_OGLAS);
        InputStream instream = oglas.getSlika().getInputstream();
        stm.setString(1, oglas.getTekst());
        stm.setBinaryStream(2, instream, oglas.getSlika().getSize());
        stm.setString(3, oglas.getVrsta());
        stm.setInt(4, oglas.getTrajanje());

        stm.executeUpdate();
        stm.close();
        return true;
    }
    catch(Exception ex){
        ex.printStackTrace();
        return false;
    }
    finally{
        ConnectionPool.getConnectionPool().checkIn((com.mysql.jdbc.Connection) conn);
    }
}

}

It breaks at DAO's

InputStream instream = oglas.getSlika().getInputstream();

with the message:

java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 7.0\work\Catalina\localhost\TravelAdvertiser\upload_fd4813af_24f6_485e_a865_5aa9be1a871d_00000019.tmp (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.getInputStream(DiskFileItem.java:188)
at org.apache.catalina.core.ApplicationPart.getInputStream(ApplicationPart.java:99)
at org.primefaces.model.NativeUploadedFile.getInputstream(NativeUploadedFile.java:45)
at net.etfbl.traveladvertiser.DAO.OglasDAO.dodajOglas(OglasDAO.java:17)
at net.etfbl.traveladvertiser.beans.OglasBean.dodajOglas(OglasBean.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...

My guess is that Tomcat cannot write to the temp folder for some reason. What is weird is that it actually contains some of the files I uploaded yesterday with the app, and it kinda just stopped working. So, maybe the problem is that I forgot some tiny thing in code (was doing a lot of changes and experimenting)?

Thanks so much!!

Gishas
  • 380
  • 6
  • 21
  • Try to set the Java.io.tmpdir to a different directory and see if this helps – Marged Sep 24 '18 at 04:56
  • 1
    Why do you think tomcat cannot save to that folder? The error is about reading. Maybe the file was written but already removed. Tried reading it in your `handleFileUpload` method? Best test and easily doable.. (and an normal step in debugging) – Kukeltje Sep 24 '18 at 13:44

1 Answers1

1

Thanks to @Kukeltje comment, I found an answer on How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable.

It is:

Also note that you should read the file contents immediately inside the abovementioned methods and not in a different bean method invoked by a later HTTP request. This is because the uploaded file contents is request scoped and thus unavailable in a later/different HTTP request. Any attempt to read it in a later request will most likely end up with java.io.FileNotFoundException on the temporary file.

What I was doing wrong, it seems, is that I tried to set Bean property in handleFileUpload method, and then read the property in the next request with another method, where it was no more available. To solve that, I saved it to the database in the handleFileUpload method, and then updated other fields through another method. Maybe not the prettiest or optimal solution, but it works for me right now.

Thanks all for your quick responses!!! I have only a few days left until my exam and this kind of help is priceless!

Gishas
  • 380
  • 6
  • 21
  • No need to answer as the duplicate contains all info. And IF you think al answer is needed, always have the courtesy to asj the person who helped you to write one.Oh and ut **is** the prettiest way to do things if you need it in the database in the end – Kukeltje Sep 25 '18 at 06:33
  • @Kukeltje Ok, thanks for the advice. I answered this primarily because of someone possibly having the same problem, and, like me, being unable to discover right answer through search ;) If you have opinion that this answer is surplus or misleading, I'll be glad to obey and delete it. – Gishas Sep 25 '18 at 08:14
  • Yes, but why would the find yours then? ;-) – Kukeltje Sep 25 '18 at 08:21