0

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


  • please help any body the image it not show in destination place – user3583947 May 01 '14 at 06:36
  • 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 May 01 '14 at 06:42
  • i am using same above code so please tell me in destination place the file have not been uploaded – user3583947 May 01 '14 at 07:06
  • Have your file field setter and getter methods?and may be help you followings link : http://stackoverflow.com/questions/14211843/how-to-save-uploaded-file/14214223#14214223 and http://stackoverflow.com/questions/18664579/recommended-way-to-save-files-uploaded-to-a-tomcat-servlet/18664715#18664715 – Hosein Masbough May 01 '14 at 07:08
  • sir i am using in jsf page
    these code in above link i am unable set to destination place
    – user3583947 May 01 '14 at 07:40
  • Hosein Masbough sir for above link i had not got any help so please provide any solution change my code plese look the my jsf page code – user3583947 May 01 '14 at 07:49

1 Answers1

0

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.

  • Gennady Nikolaev sir i am trying these above code defined by you but i am not show the image in destination place – user3583947 May 01 '14 at 12:29
  • I am not sure I exactly understand what do you mean. Not show - you mean there is no image at destination? Some exceptions occur? – Gennady Nikolaev May 01 '14 at 14:24
  • Gennady Nikolaev sir i am found my issue it is browser issue i am using IE11 browser it work fine in mozila and IE9,IE10 so thanks sir – user3583947 May 02 '14 at 05:45
  • these image show in destination place TEMP folder so but how to show these image in my sql database i am using mysql 5.0 please help any one – user3583947 May 02 '14 at 05:47
  • these image upload destination on IE10 browser but not upload in IE9 browser plese help i got stuck here – user3583947 May 02 '14 at 12:36