gpt4 book ai didi

eclipse - Tomcat 无法保存到临时文件夹?

转载 作者:行者123 更新时间:2023-11-28 22:47:45 25 4
gpt4 key购买 nike

<分区>

我正在为期末考试制作一个简单的 JSF 应用程序。它基本上是一个带有图片和一些基本数据上传(文本等)的页面应用程序。图片上传部分出现问题。我使用 Primefaces 单个文件上传:

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

}

它在 DAO 中崩溃了

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 出于某种原因无法写入临时文件夹。奇怪的是,它实际上包含我昨天通过该应用程序上传的一些文件,而且它有点停止工作了。所以,也许问题是我忘记了代码中的一些小东西(做了很多更改和试验)?

非常感谢!!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com