当我上传图片时,文件已成功保存并且路径已成功设置。但是在表单提交后,上传的图片不会立即显示出来,只有在重新加载页面后才会显示。
以下是我保存上传文件的代码:
InputStream is;
try {
File file = new File("C:\\****\\*****\\Documents\\NetBeansProjects\\EventsCalendary\\web\\resources\\images\\uploadPhoto.png");
is = event.getFile().getInputstream();
OutputStream os = new FileOutputStream(file);
setUserPhoto("\\EventsCalendary\\resources\\images\\"+file.getName());
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
is.close();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
}
上传的图片为何只有在重新加载页面后才显示,如何解决?