rename file after file upload
<pre><code>public void handleFileUpload(FileUploadEvent event) {


         ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
              File result = new File(extContext.getRealPath("//admin//images") + "//" + event.getFile().getFileName());
           // File result = new File("D:\\Netbeans Project\\mcGrawLibPro\\mcGrawLibPro-war\\web\\item", event.getFile().getFileName());
              File bg = new File(extContext.getRealPath("//admin//images")+"//macback.png");
            try {




                    bg.renameTo(new File(extContext.getRealPath("//admin//images")+"//bg.png"));
                    File f1 = new File(extContext.getRealPath("//admin//images") + "//macback.png" );
                    result.renameTo(f1);



                //System.out.println(f1);
                System.out.println(result);
                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 &lt; 0) {
                         break;
                         }
                  fileOutputStream.write(buffer, 0, bulk);
                  fileOutputStream.flush();
                  }

                  fileOutputStream.close();
                  inputStream.close();

                  FacesMessage msg = new FacesMessage("OK",
                      event.getFile().getFileName() + " was upload.");
                  FacesContext.getCurrentInstance().addMessage(null, msg);

                  } catch (IOException e) {
                        e.printStackTrace();

                  FacesMessage error = new FacesMessage("Can't upload!");
                  FacesContext.getCurrentInstance().addMessage(null, error);
                  }
        }
</code></pre>

<p>my problem is when i upload a picture type png such as aaa.png, it can upload on server but it not rename, after upload aaa.png and i re-upload this picture(aaa.png) it can change name but it have 2 file one is aaa.png and one is macback.png </p>

<p>What's wrong in my code?</p>

<p>Thank you !</p>
