views:

51

answers:

1

Hello once again i ask question on stackOverflow :D

how can i upload file with JSF using primefaces? i have method handle Upload Image

 public void handleFileUpload(FileUploadEvent event) {
                        ExternalContext extContext = FacesContext.getCurrentInstance().
                                             getExternalContext();
                File result = new File(extContext.getRealPath
                 ("//admin//item") + "//" + 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);
                  }
        }

it work well but it just work when server deploy, when i upload image to server , folder //admin/item/ have image but when i re-start server i can't find image i was uploaded

and how can i display thumbnails for each item with each own image

A: 

but when i re-start server i can't find image i was uploaded

That's normal in most servletcontainer configurations. When you restart the server or redeploy the webapp, any previously expanded webapp files will be deleted and the WAR will be re-expanded. You shouldn't store uploaded files in the expanded folder for the case you'd like to keep them longer than the webapp context lives.

The normal practice is to store them in a fixed path outside the webapp context, e.g. /var/webapp/upload.

File file = new File("/var/webapp/upload", event.getFile().getFileName());

Unrelated to the problem, I'd suggest to make use of File#createTempFile() to avoid that another uploaded file which has -by coincidence- the same filename will overwrite any previously uploaded one.

BalusC
THANK YOU VERY MUCH, but how can i display thumbnail? i use `h:graphicImage value"var/webapp/upload/#{getName}" id="thumb"` and re-render it , it not work , but with h:inputText i can get name of image, please help me ! thank you
MYE
Either map it as another webapp context so that it's accessible by for example http://example.com/upload or use a servlet which streams the bytes from disk to response.
BalusC
See also my answer on [this question](http://stackoverflow.com/questions/3723718/what-is-the-correct-path-i-need-to-see-my-uploaded-image) for more detail on either of the approaches.
BalusC
thank Balus i know my problem, i save image url to `/var/webapp/upload` and image will display when i re-deploy it on server. i very like your blog it helpful for me ^^
MYE