gpt4 book ai didi

java - 文件上传后重命名文件

转载 作者:行者123 更新时间:2023-12-01 16:00:17 25 4
gpt4 key购买 nike

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

我的代码有什么问题?

谢谢!

最佳答案

我认为您正在尝试执行以下操作:

  1. 上传文件时,您希望将其命名为 macback.png,而不是其原始文件名。
  2. 上传第二个文件时,您需要先将 macback.png 重命名为 bg.png,然后将上传的文件另存为 macback.png。

如果是这种情况,首先您需要测试 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);
}
}

关于java - 文件上传后重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4141348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com