6
今天我遇到了PrimeFaces FileUpload的問題。它工作的很好,但是這些文件存儲在JBoss的臨時目錄中,所以無論何時重新部署應用程序,或者只是將源文件提交到svn,我上傳的所有圖像都不再存在。所以我想問一下,是否有辦法將圖像保存到戰爭項目的「源代碼」目錄中。j2ee primefaces fileupload文件保存目標
我handleFileUpload方法:
public String getUrlBase() {
return FacesContext.getCurrentInstance().getExternalContext().getRealPath("//upload");
}
public void handleFileUpload(FileUploadEvent event) {
new File(getUrlBase() + "/" + album.getId()).mkdirs();
File result = new File(getUrlBase() + "/" + album.getId() + "/" + event.getFile().getFileName());
try {
FileOutputStream fileOutputStream = new FileOutputStream(result);
byte[] buffer = new byte[BUFFER_SIZE];
int bulk;
InputStream inputStream = event.getFile().getInputstream();
while (true) {
bulk = inputStream.read(buffer);
if (bulk < 0) {
break;
}
fileOutputStream.write(buffer, 0, bulk);
fileOutputStream.flush();
}
fileOutputStream.close();
inputStream.close();
FacesMessage msg = new FacesMessage("Succesful",
event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
} catch (IOException e) {
e.printStackTrace();
FacesMessage error = new FacesMessage("The files were not uploaded!");
FacesContext.getCurrentInstance().addMessage(null, error);
}
}
編輯:另一個問題
我的視圖文件:
<h:form enctype="multipart/form-data" prependId="false">
<p:growl id="messages" showSummary="true" showDetail="true" />
<p:fileUpload fileUploadListener="#{photo.handleFileUpload}"
update="messages" sizeLimit="1073741824"
multiple="true" label="choose" allowTypes="*.jpg;*.png;*.gif;"
description="Images" />
</h:form>
當我添加更新= 「消息」 的文件上傳我得到
javax.servlet.ServletException: null source
每當我嘗試用fileUpload打開頁面。你不知道如何擺脫它?
根據使用指南,這只是一個地方,臨時文件的存儲位置。我需要上傳一些圖片,然後將它們顯示在照片庫中...所以我想將它們存儲在web根目錄(例如WEB-INF目錄中)的「上傳」目錄中。所以當我將源代碼提交給svn時,其他人也會在圖庫中添加圖片。 我試着將uploadDIrectory設置爲/上傳,但似乎不起作用。 – 2010-12-05 13:23:49