Reputation: 279
I have using jsf2.0 in eclipse ide.I have a problem to store a image in local folder.The images are stored in local folder with 0 bytes.The images are read and write into 0 bytes.
Backing 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();
}
}
The Error Occurs:
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)
Upvotes: 0
Views: 1182
Reputation: 1108642
As answered so many times before, you should not store/locate uploaded files in public web content. You should be using an absolute path instead. E.g.
File file = new File("/path/to/images", filename);
// ...
The uploaded file is not magically present in the expanded WAR, so reading it with FileInputStream
won't work at all. You'd have to read it from event.getFile().getInputStream()
and then write it to an arbitrary OutputStream
such as FileOutputStream
.
But now your real problem is more clear than in your previous questions. You seem to want to save the uploaded file in the database. There's then no need to store the file on local disk file system first. Just stream the received bytes to a byte[]
directly and the save it in the DB.
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());
Upvotes: 3