Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6781215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:35:52+00:00 2026-05-26T16:35:52+00:00

I have using jsf2.0 in eclipse ide.I have a problem to store a image

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:35:53+00:00Added an answer on May 26, 2026 at 4:35 pm

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to jsf frame work.I am using jsf2.0 in eclipse ide.I have
I have been using Eclipse as an IDE for a short amount of time
I added and have been using the faces-config.xml in my Netbeans 6.9/JSF2.0 project due
My JSF2 application is fully internationalized, using a ResourceBundle. Now i have a lot
I have a web project using jsf2 implementing a gaming web site on tomcat7.
I have a problem with following tutorial: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/ In faces-context file you have to
I am using JSF2.0 with Richfaces 4.0.0.Final for a web application. I have a
I'm using JSF2 on GlassFish 3. I have a form that accepts and optional
I am running an application using JSF2.0 and Primefaces 2.2RC2 I have run the
I'm using RichFaces on my JSF2 application, and I need a way to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.