我使用了简单模式,glassfish 3.0和primefaces 3.0
支持豆
private UploadedFile文件;private字符串destination="D:\temp\";
public void upload(FleUploadEvent事件({
System.out.println("upload");
FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
try {
copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
}
catch (IOException e)
{
e.printStackTrace();
}
}
System.out.println("uploaf finished");
public void copyFile(String fileName,中的InputStream({尝试{
// write the inputStream to a FileOutputStream
OutputStream out = new FileOutputStream(new File(destination + fileName));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.flush();
out.close();
System.out.println("New file created!");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
这是我的jsf页面
在您的示例中,"D:\tmp\"后面跟着上载文件的完整文件名,即"C:\Users\admin\Desktop\ulcerimage.jpg"。这是不正确的,并导致错误。您所需要的只是处理上传的文件名:提取没有路径信息的实际名称。例如,您可以使用以下内容:
if (fileName.indexOf("/")>0){
fileName = fileName.substring(fileName.lastIndexOf("/"));
}
OutputStream out = new FileOutputStream(new File(destination + fileName));
在copyFile方法中。