File upload 将图像文件保存在jsf primefaces项目的特定目录中
我想将byte[]文件保存到特定目录中: 我从这个方法中得到:File upload 将图像文件保存在jsf primefaces项目的特定目录中,file-upload,jsf-2,primefaces,directory,File Upload,Jsf 2,Primefaces,Directory,我想将byte[]文件保存到特定目录中: 我从这个方法中得到: public void setUploadedPicture(UploadedFile uploadedPicture) { System.out.println("set : "+uploadedPicture.getFileName()+" size : "+uploadedPicture.getSize()); this.uploadedPicture = uploadedPicture; }
public void setUploadedPicture(UploadedFile uploadedPicture)
{
System.out.println("set : "+uploadedPicture.getFileName()+" size : "+uploadedPicture.getSize());
this.uploadedPicture = uploadedPicture;
}
我通过以下方式访问字节[]:
uploadedPicture.getContents()
我对此进行了测试,但没有结果
如何将其保存到项目内部或外部的特定目录中
多谢各位
*********编辑************
以下是代码,但有时会出现错误:
public void setUploadedPicture(UploadedFile uploadedPicture)
{
System.out.println("set : "+uploadedPicture.getFileName()+" size : "+uploadedPicture.getSize());
this.uploadedPicture = uploadedPicture;
InputStream inputStr = null;
try {
inputStr = uploadedPicture.getInputstream();
} catch (IOException e) {
e.printStackTrace();
}
//create destination File
String destPath = "C:\\"+uploadedPicture.getFileName();
File destFile = new File(destPath);
//use org.apache.commons.io.FileUtils to copy the File
try {
FileUtils.copyInputStreamToFile(inputStr, destFile);
} catch (IOException e) {
e.printStackTrace();
}
}
感谢您提供这部分代码(我将对其进行测试),我不使用这种方法,因为我使用简单的uploadfile模式,然后我直接使用byte[]contentatbegin,
getInputStream()
无论采用何种上传模式,都应该可以正常工作。唯一的区别是它不会将整个文件放在服务器的内存中,因此效率更高。@pantominas我测试了你的代码,但问题是有时我在提交时会出现以下错误:C:\Users\user\AppData\Local\Temp\upload\uuu54ab5703\u13da6c8fc2c\u8000\u00000016.tmp(Le fichier spécifi est introvable)
public void handleFileUpload(FileUploadEvent event) {
//get uploaded file from the event
UploadedFile uploadedFile = (UploadedFile)event.getFile();
//create an InputStream from the uploaded file
InputStream inputStr = null;
try {
inputStr = uploadedFile.getInputstream();
} catch (IOException e) {
//log error
}
//create destination File
String destPath = "your path here";
File destFile = new File(destPath);
//use org.apache.commons.io.FileUtils to copy the File
try {
FileUtils.copyInputStreamToFile(inputStr, destFile);
} catch (IOException e) {
//log error
}
}