Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  public void handleFileUpload(FileUploadEvent event) {
  2.              
  3.         FacesContext facesContext = FacesContext.getCurrentInstance();
  4.         ExternalContext externalContext = facesContext.getExternalContext();
  5.         HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
  6.        
  7.         System.out.println("path:"+externalContext.getRealPath("/upload/"));
  8.        
  9.         System.out.println("file solo:" +event.getFile().getFileName());
  10.                
  11.        File result = new File(externalContext.getRealPath("/upload/")+File.separator + event.getFile().getFileName() );
  12.        
  13.         System.out.println("final file:"+result.getName());
  14.  
  15.        
  16.           current.setEmpLog(event.getFile().getFileName());
  17.         try {
  18.  
  19.             FileOutputStream fileOutputStream = new FileOutputStream(result);
  20.  
  21.             byte[] buffer = new byte[BUFFER_SIZE];
  22.  
  23.             int bulk;
  24.  
  25.             // Here you get uploaded picture bytes, while debugging you can see that 34818
  26.             InputStream inputStream = event.getFile().getInputstream();
  27.            
  28.             while(true) {
  29.  
  30.                 bulk = inputStream.read(buffer);
  31.  
  32.                 if (bulk < 0) {
  33.  
  34.                     break;
  35.  
  36.                 } //end of if
  37.  
  38.                 fileOutputStream.write(buffer, 0, bulk);
  39.                 fileOutputStream.flush();
  40.  
  41.             } //end fo while(true)
  42.  
  43.             fileOutputStream.close();
  44.             inputStream.close();
  45.  
  46.             FacesMessage msg = new FacesMessage("Succesful",event.getFile().getFileName() + " is uploaded.");
  47.             FacesContext.getCurrentInstance().addMessage(null, msg);
  48.  
  49.         }  catch (IOException e) {
  50.  
  51.             e.printStackTrace();
  52.  
  53.             FacesMessage error = new FacesMessage("The files were not uploaded!");
  54.             FacesContext.getCurrentInstance().addMessage(null, error);
  55.  
  56.         }
  57.          
  58.    
  59.                
  60.     }