代码之家  ›  专栏  ›  技术社区  ›  Gishas

Tomcat无法保存到临时文件夹?[副本]

  •  0
  • Gishas  · 技术社区  · 6 年前

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

    备份bean代码:

    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);
        }
    }
    

    用于添加到数据库的DAO方法,实际上是问题所在:

    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);
        }
    }
    
    }
    

    它在刀口断了

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

    带着信息:

    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)
    ...
    

    我的猜测是Tomcat由于某种原因无法写入temp文件夹。奇怪的是,它实际上包含了我昨天用这个应用上传的一些文件,而且它刚刚停止工作。所以,也许问题是我忘记了代码中的一些小东西(做了很多修改和试验)?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Gishas    6 年前

    感谢@Kukeltje的评论,我找到了 How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable

    它是:

    还要注意,您应该立即在上述方法中读取文件内容,而不是在稍后的HTTP请求调用的其他bean方法中读取。这是因为上载的文件内容是请求范围的,因此在稍后的/不同的HTTP请求中不可用。在以后的请求中读取它的任何尝试都很可能在临时文件上出现java.io.FileNotFoundException。

    我做错了,似乎是我试图在handleFileUpload方法中设置Bean属性,然后在下一个请求中使用另一个方法读取该属性,而该方法不再可用。为了解决这个问题,我用handleFileUpload方法将它保存到数据库中,然后通过另一个方法更新其他字段。也许不是最漂亮或最理想的解决方案,但它现在对我有效。

    谢谢大家的快速反应!!!我离考试只剩下几天了,这种帮助是无价的!