0
votes

i used simple mode, glassfish 3.0 and primefaces 3.0

Backing Bean

private UploadedFile file; private String destination="D:\temp\";

public void upload(FleUploadEvent event) {

    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 {

           // 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


1
please help any body the image it not show in destination place - user3583947
the following message is show not generated any exception or error only simple message "D:\tmp\C:\Users\admin\Desktop\ulcerimage.jpg (The filename, directory name, or volume label syntax is incorrect)" - user3583947
i am using same above code so please tell me in destination place the file have not been uploaded - user3583947
Have your file field setter and getter methods?and may be help you followings link : stackoverflow.com/questions/14211843/how-to-save-uploaded-file/… and stackoverflow.com/questions/18664579/… - Hosein Masbough
sir i am using in jsf page <p:fileUpload fileUploadListener="#{fileUploadController.upload}" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" sizeLimit="100000"/><br/> these code in above link i am unable set to destination place - user3583947

1 Answers

0
votes

In your example "D:\tmp\" is followed by the full filename of the uploaded file, i.e. "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.