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
我的代码出了什么问题?
谢谢!
答案 0 :(得分:1)
以下是我认为你要做的事情:
如果是这种情况,首先需要测试是否存在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)
好的,让我们看看这次是否得到了它:
如果是这种情况,请尝试:
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
。