【发布时间】:2017-10-20 21:09:51
【问题描述】:
我有这样的实体
@Entity
public class myFiles implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idmyFiles;
@Lob
private File myFile;
getters and setters...
我有一个似乎可以使用的上传表单,当我上传一个文件时,我可以在 postgtes 表中看到它。
在表格中(我使用的是 primefaces)我有这个
<p:fieldset id="myFiles" class="recuadro" legend = "Upload a fileos">
<p:fileUpload value="#{abmBean.fichero}" mode="simple" />
<p:commandButton id="btnSubir" ajax="false" process="@all" update="@all"
action="#{abmBean.uploadFile()}" value="Subir" />
</p:fieldset>
这是上传文件的方法
public void uploadFile() {
File destFile = new File("/tmp/"+fichero.getFileName());
FileUtils.copyInputStreamToFile(fichero.getInputstream(), destFile);
/* Then I just create a myFile object F and just F.setFile(destFile) */
}
它似乎有效
要下载文件我有这个方法
public downloadFile(int id) {
/* the int id is the id of the myfile object F */
/* I retrieve from the database the F object and then this*/
File fichero = new File("/tmp/"+F.getFile.getName());
FileUtils.copyFile(F.getFile, fichero);
Faces.sendFile(fichero, true);
}
这很好用,我把文件拿回来了
但问题是这样的,我意识到我上传的文件也被复制到 /tomcat7/bin 文件夹,如果我从那里删除文件,那么当我想下载它时,我得到一个 fileNotFoundException。
我只需要能够为用户提供数据库中的文件
我在 Debian 上使用 Tomcat7、Java 1.6(是的很旧)Primefaces 6、Postgresql 9.1 和 JSF2
【问题讨论】:
-
您的标题具有误导性,我没有看到任何 OmniFaces 组件,而是 PrimeFaces(如您的标签中所述)。另外,请检查您管理文件上传的支持 bean 是否有
@MultipartConfigannotation。另一方面,请检查您的 PrimeFaces 配置中的默认上传文件夹 (documentation link, page 223)。我假设myFiles实体中的 getter 正在从正确的文件夹中获取 -
谢谢我改了标题,我的 backingbean 没有 @MultipartConfig ,我会检查你建议的手册页
-
文件在数据库中是二进制而不是字符串路径
-
所以下载文件时文件只在/tomcat7/bin 中结束?那么你 Q 中的所有上传代码都是无关紧要的。请巴罗做这个问题。 Postgress 99.999% 不相关。
-
正如 Kukeltje 提到的,您数据库中的文件与您的问题无关。我怀疑 PrimeFaces 临时文件夹或类似的配置错误。
FileUtils.copyFile(F.getFile, fichero);中的F.getFile怎么样?它指的是哪里?
标签: jsf primefaces