这是我试图在这里完成的后续工作/sf/ask/511974571/
我已经设法将图像作为UploadedFile
对象,但我似乎无法将其保存到磁盘.我想在本地保存它(C:\Temp
例如),这样当我运行我的应用程序时,我可以test.jpg
从我的桌面上传文件(例如)并将其保存在服务器上(例如,在C:\Temp
).
我的bean非常简单:
import org.apache.myfaces.custom.fileupload.UploadedFile;
public class PatientBB {
private UploadedFile uploadedFile;
public UploadedFile getUploadedFile(){
return this.uploadedFile;
}
.
public void setUploadedFile(UploadedFile uploadedFile){
this.uploadedFile = uploadedFile;
}
.
public String actionSubmitImage(){
//This is th part I need help with. how do I save it in my C?
}
Run Code Online (Sandbox Code Playgroud)
我非常感谢所有的帮助,谢谢!
据我所知,根据javaDoc,你应该可以做到
uploadedFile.getInputStream();
Run Code Online (Sandbox Code Playgroud)
然后将数据从那里推送到FileOutputStream.
伪:
InputStream is = uploadedFile.getInputStream();
byte[] buffer = new byte[uploadedFile.getLength()); //This can be more space-efficient if necessary
is.read(buffer);
File f = new File("C:\\tmp\\" + uploadedFile.getFilename());
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
Run Code Online (Sandbox Code Playgroud)
那有意义吗?这就是你要找的东西吗?