0

我有一个网络应用程序,想将图像保存到我的数据库,然后想从数据库中检索保存的图像。我可以通过 PrimeFaces fileUpload 将图像上传到我的网络应用程序,并将图像属性保存到我的数据库中。但是,我无法将上传的图像保存到我的网络应用程序的某个目录中,以便稍后我可以从数据库中检索其路径和属性以及从目录中检索图像。我收到以下错误:

javax.el.ELException: /compositions/defineType.xhtml @122,56 fileUploadListener="#{typeBean.handleTypeImageUpload}": java.io.FileNotFoundException: C:\Users\mta\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MyWebApp\webapp\images\uploadedImages\alarmPassive.gif

我的文件上传监听器:

public void handleTypeImageUpload(FileUploadEvent event) throws IOException {  
    UploadedFile file = event.getFile();
    Type newTypeProp = new Type();
    newTypeProp .setTypeName("someName");
    newTypeProp .setIconFileName(imageFile.getFileName());
    newTypeProp .setIcon(imageFile.getContents());

    databaseDao.saveType(newTypeProp );

    FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
    FacesContext.getCurrentInstance().addMessage(null, msg);  

    String prefix = FilenameUtils.getBaseName(file.getFileName());
    String suffix = FilenameUtils.getExtension(file.getFileName());

    File fileToDirectory = File.createTempFile(prefix + "-", "." + suffix, new File("C:\\temp"));

    InputStream is = event.getFile().getInputstream();
    OutputStream out = new FileOutputStream(fileToDirectory);
    byte buf[] = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0)
        out.write(buf, 0, len);
    is.close();
    out.close();
}

我保存到 DB 的类型支持 bean:

public class Type {
    private Integer typeId;
    private String typeName;
    private byte[] icon;
    private String iconFileName;

    // getters & setters
    ...
}

xhtml文件上传:

<p:fileUpload style="font-size:11px;" fileUploadListener="#{typeBean.handleTypeImageUpload}" 
    mode="advanced" dragDropSupport="true"  
    sizeLimit="100000" fileLimit="3" update="messages"
    allowTypes="/(\.|\/)(gif|jpe?g|png)$/" 
    label="#{general.select}" uploadLabel="#{general.upload}" 
    cancelLabel="#{general.cancel}">
</p:fileUpload>

现在,我怎样才能将图像文件保存到我的网络应用程序的某个目录中,以便稍后我可以得到它?任何帮助将不胜感激。

4

0 回答 0