简体   繁体   English

在primefaces 3和jsf中,我上传了文件,但在目标位置未显示图像

[英]in primefaces 3 and jsf i upload the file but in destination place the image is not show

i used simple mode, glassfish 3.0 and primefaces 3.0 我使用简单模式,glassfish 3.0和primefaces 3.0

Backing Bean 菜豆

private UploadedFile file; 私人UploadedFile文件; private String destination="D:\\temp\\"; private String destination =“ D:\\ temp \\”;

public void upload(FleUploadEvent event) { 公共无效上传(FleUploadEvent事件){

    System.out.println("upload");
    FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
    FacesContext.getCurrentInstance().addMessage(null, msg);
        try  {
        copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
           } 
     catch (IOException e)
    {
        e.printStackTrace();
    }
  }
    System.out.println("uploaf finished"); 

public void copyFile(String fileName, InputStream in) { try { public void copyFile(String fileName,InputStream in){试试{

           // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(destination + fileName));

         int read = 0;
       byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
             out.write(bytes, 0, read);
         }

         in.close();
       out.flush();
         out.close();

    System.out.println("New file created!");
        } catch (IOException e) {
      System.out.println(e.getMessage());
      }

} }

this is my jsf page 这是我的jsf页面


In your example "D:\\tmp\\" is followed by the full filename of the uploaded file, ie "C:\\Users\\admin\\Desktop\\ulcerimage.jpg". 在您的示例中,“ D:\\ tmp \\”后跟上载文件的完整文件名,即“ C:\\ Users \\ admin \\ Desktop \\ ulcerimage.jpg”。 This is incorrect and leads to the error. 这是不正确的,并导致错误。 All what you need is to process uploaded filename: extract actual name without path information. 您所需要做的就是处理上载的文件名:提取不带路径信息的实际名称。 You can use for example the following: 您可以使用以下示例:

if (fileName.indexOf("/")>0){
  fileName = fileName.substring(fileName.lastIndexOf("/"));
}
OutputStream out = new FileOutputStream(new File(destination + fileName));

in copyFile method. 在copyFile方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM