文件上传后重命名文件

时间:2010-11-10 04:54:11

标签: java

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 < 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);
                  }
        }

我的问题是当我上传图片类型png如aaa.png时,它可以在服务器上传但不重命名,上传aaa.png后我重新上传这张图片(aaa.png)它可以更改名称但它有2个文件,一个是aaa.png,一个是macback.png

我的代码出了什么问题?

谢谢!

3 个答案:

答案 0 :(得分:1)

以下是我认为你要做的事情:

  1. 上传文件时,您想将其称为macback.png,而不是原始文件名。
  2. 上传第二个文件时,首先要将macback.png重命名为bg.png,然后将上传的文件保存为macback.png。
  3. 如果是这种情况,首先需要测试是否存在macback.png文件,如果存在则重命名。然后为macback.png文件创建一个File对象,并打开一个FileOutputStream到该文件,将上传的文件写入。

    这样的事情:

    public void handleFileUpload(FileUploadEvent event) {
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        File result = new File(extContext.getRealPath("//admin//images") + "//macback.png");
        if(result.exists()) {
            result.renameTo(new File(extContext.getRealPath("//admin//images")+"//bg.png"));
        }
    
        try {
            File targetFile = new File(extContext.getRealPath("//admin//images") + "//macback.png" );
    
            FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
            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("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);
        }
    }
    

答案 1 :(得分:0)

好的,让我们看看这次是否得到了它:

  1. 您希望第一个A.png保存为A.png。
  2. 第二次上传A.png时,您希望将第一个A.png重命名为macback.png,将第二个A.png重命名为A.png。
  3. 如果是这种情况,请尝试:

    public void handleFileUpload(FileUploadEvent event) {
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        File result = new File(extContext.getRealPath("//admin//images") + "//" + event.getFile().getFileName());
        if(result.exists()) {
            result.renameTo(new File(extContext.getRealPath("//admin//images")+"//mackback.png"));
        }
    
        try {
            File targetFile = new File(extContext.getRealPath("//admin//images") + "//" + event.getFile().getFileName() );
    
            FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
            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("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);
        }
    }
    

答案 2 :(得分:0)

我认为问题是您要过早地将result重命名为f1。在您调用result.renameTo(f1)时,您还没有创建result引用的文件,因此重命名无效。在磁盘上存在文件之前,您无法重命名该文件。

现在,您不需要重命名result。由于您尚未创建该文件,因此您只需使用f1作为FileOutputStream的参数,而不是result