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 7309119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:40:29+00:00 2026-05-28T23:40:29+00:00

hi i am building a dynamic web project in which the welcome page have

  • 0

hi i am building a dynamic web project in which the welcome page have struts2 file tag now i want to store that specified file to mysql database would some one help me…

Thanks in advance.

Here is the Code i developed but it takes the file parameter statically means manually i am specifying path. but it should take path from the struts 2 file tag see the java class u will get it..

public class FileUploadACtion 
{

    public String execute() throws IOException
{
System.out.println("Hibernate save image into database");
    Session session = HibernateUtil.getSessionFactory().openSession();

    session.beginTransaction();

    //save image into database
    File file = new File("C:\\mavan-hibernate-image-mysql.gif");
    byte[] bFile = new byte[(int) file.length()];

    try {
     FileInputStream fileInputStream = new FileInputStream(file);
     //convert file into array of bytes
     fileInputStream.read(bFile);
     fileInputStream.close();
    } catch (Exception e) {
     e.printStackTrace();
    }

    FileUpload tfile = new FileUpload();
    avatar.setImage(bFile);

    session.save(tfile);

    //Get image from database
    FileUpload tfile2 =         (FileUpload)session.get(FileUpload.class,FileUpload.getAvatarId());
    byte[] bAvatar = avatar2.getImage();
    try{
        FileOutputStream fos = new FileOutputStream("C:\\test.gif"); 
        fos.write(bAvatar);
        fos.close();
    }catch(Exception e){
        e.printStackTrace();
    }

    session.getTransaction().commit();
}

}

  • 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-28T23:40:31+00:00Added an answer on May 28, 2026 at 11:40 pm

    You should be storing the image in the table as a BLOB type. Lets assume you have a Person class with the an image of the person stored in the DB. If you want to map this, just add a property in your person POJO that holds the image.

    @Column(name="image")
    @Blob
    private Blob image;
    

    When you display it, convert it to a byte[] and show.

    private byte[] toByteArray(Blob fromImageBlob) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
          return toByteArrayImpl(fromImageBlob, baos);
        } catch (Exception e) {
        }
        return null;
      }
    
    
    
    private byte[] toByteArrayImpl(Blob fromImageBlob, 
          ByteArrayOutputStream baos) throws SQLException, IOException {
        byte buf[] = new byte[4000];
        int dataSize;
        InputStream is = fromImageBlob.getBinaryStream(); 
    
        try {
          while((dataSize = is.read(buf)) != -1) {
            baos.write(buf, 0, dataSize);
          }    
        } finally {
          if(is != null) {
            is.close();
          }
        }
        return baos.toByteArray();
      }
    

    You can see the below examples to know more about it.

    1. http://i-proving.com/space/Technologies/Hibernate/Blobs+and+Hibernate
    2. http://snehaprashant.blogspot.com/2008/08/how-to-store-and-retrieve-blob-object.html
    3. http://viralpatel.net/blogs/2011/01/tutorial-save-get-blob-object-spring-3-mvc-hibernate.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a dynamic web application in Eclipse using Struts2 and OC4J 10.1.3.3.0. I'm
Theres a web page I'm making which content is very dynamic and so I'm
I have been asked to look into dynamic web page development, and there appears
I am building a dynamic web project using eclipse j2ee helios 3 version. Everything
I am building a system in Java/Groovy that involves dynamic invocation of Web services.
I am currently building a very dynamic table for a list application, which will
I am building a website that would serve dynamic content. All communication between server/browser
building a site using PHP and MySQL that needs to store a lot of
We are building web applications for the iPhone that work offline. But we are
I'm building a multi-tenant web application where for security concerns, we need 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.