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!!