我在eclipse ide中使用了jsf2.0。我有一个问题是将图像存储在本地文件夹中。图像存储在0字节的本地文件夹中。图像被读写为0字节。
支持Bean:
public class uploadImageAction {
public void handleFileUpload(FileUploadEvent event) throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
PhotoBean photoBean = (PhotoBean) context.getApplication().evaluateExpressionGet(context, "#{photoBean}", PhotoBean.class);
PhotoBean photoBean2 = (PhotoBean) context.getApplication().evaluateExpressionGet(context, "#{photoBean2}", PhotoBean.class);
ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();
File file = new File(extContext.getRealPath(event.getFile().getFileName()));
System.out.println("Hibernate save image into database");
Session session = HibernateUtil1.getSessionFactory().openSession();
session.beginTransaction();
//save image into database
//File file = new File("D:\\1.gif");
//byte[] bFile = new byte[(int) file.length()];
//System.out.println("DDDDDDDDDDDD"+bFile);
byte[] bfile = new byte[(int)file.length()];
System.out.println("DDDDDDDDDDDD"+bfile);
try {
FileInputStream fileInputStream = new FileInputStream(file);
//convert file into array of bytes
fileInputStream.read(bfile);
fileInputStream.close();
} catch (Exception e) {
System.out.println("Cannnoy");
e.printStackTrace();
}
String urlImage = file.toString();
String imageName = event.getFile().getFileName();
photoBean.setPathName(file);
//file = photoBean.getPathName();
photoBean.setImageName(imageName);
imageName = photoBean.getImageName();
photoBean.setUrl(urlImage);
urlImage = photoBean.getUrl();
// PhotoDaoService photoDaoService = new PhotoDaoService();
System.out.println("+++++++++++++++++++++++++"+file);
System.out.println("CHECK BYTES"+bfile);
// uploadImageBean upldImageBean = new uploadImageBean();
//upldImageBean.setImageByte(bfile);
photoBean.setByteImage(bfile);
//avatar.setImage(bFile);
// System.out.println("dfdffdf"+upldImageBean.setImageByte(bFile));
session.save(photoBean);
//Get image from database
photoBean2 = (PhotoBean)session.get(PhotoBean.class, photoBean.getId());
byte[] bImage = photoBean2.getByteImage();
System.out.println("QQQQQQQQQQQQQQQ"+bImage);
int i=0;
try{
FileOutputStream fos = new FileOutputStream("e:/out/out"+0+".jpg");
fos.write(bImage);
fos.close();
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName()
+ " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}catch(Exception e){
e.printStackTrace();
System.out.println("UPLOAD DISPLAY EXCEPTION");
}
session.getTransaction().commit();
}
}
发生错误:
Hibernate save image into database
bytes[B@12fb7ef
java.io.FileNotFoundException: E:\Mecherie_project\.metadata\.plugins \org.eclipse.wst.server.core\tmp0\wtpwebapps\image_web\baby.jpg (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at image.uploadImageAction.handleFileUpload(uploadImageAction.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
答案 0 :(得分:3)
经过回答many times before,您应不在公共网页内容中存储/定位上传的文件。您应该使用绝对路径。 E.g。
File file = new File("/path/to/images", filename);
// ...
上传的文件不会神奇地存在于展开的WAR中,因此使用FileInputStream
阅读它根本不会有效。您必须从event.getFile().getInputStream()
阅读,然后将其写入OutputStream
等任意FileOutputStream
。
但是现在你的真正的问题比以前的问题更清晰了。您似乎想要将上载的文件保存在数据库中。然后不需要首先将文件存储在本地磁盘文件系统上。只需将收到的字节直接流式传输到byte[]
,然后将其保存在数据库中。
InputStream input = event.getFile().getInputstream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = input.read(buffer)) > -1;) {
output.write(buffer, 0, length);
}
photoBean.setByteImage(output.toByteArray());