6 Replies Latest reply on Jan 29, 2009 7:24 AM by 807581

    Image uploading using struts and hibernate...

      Hi..I did image uploading using struts and hibernate....try it and reply..


      --Below is the script for image type validation.

      var fileTypes=["bmp","gif","png","jpg","jpeg","tif","tiff","jif","jfif","jp2","jpx","j2k","j2c","fpx","pcd" ];
      function preview(what){
      var source=what.value;

      var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
      for (var i=0; i<fileTypes.length; i++) if (fileTypes==ext) break;
      var globalPic=new Image();
      if (!(i<fileTypes.length)){
           alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));
                return false;
           return true;


      --You have to write relevent code for view.For browse filed i given below

      <INPUT type=file name="fileUpload" onchange="preview(this)">


      import org.apache.struts.action.Action;
      import org.apache.struts.action.ActionForm;
      import org.apache.struts.action.ActionForward;
      import org.apache.struts.action.ActionMapping;
      public class Regstep5Form extends ActionForm {

           private FormFile fileUpload;
           public FormFile getFileUpload() {
           return fileUpload;
           public void setFileUpload(FormFile fileUpload) {
           this.fileUpload = fileUpload;


      import org.apache.struts.action.Action;
      import org.apache.struts.action.ActionForm;
      import org.apache.struts.action.ActionForward;
      import org.apache.struts.action.ActionMapping;

      import org.apache.struts.upload.FormFile;

      public class Regstep5Action extends Action {
           public ActionForward execute(ActionMapping mapping, ActionForm form,

      HttpServletRequest request, HttpServletResponse response)

      throws Exception, ServletException {
                     Regstep5Form rf= (Regstep5Form)form;
                     FormFile myFile = rf.getFileUpload();
           String contentType = myFile.getContentType();
           String fileName = myFile.getFileName();
           int fileSize = myFile.getFileSize();
           byte[] fileData = myFile.getFileData();
                String result=RegPersonalDetails.process(rf);
                     System.out.println("result taken");
                     return mapping.findForward("success");


      import com.model.orm.TempPhoto;
      import com.util.HibernateUtil;
      import org.hibernate.*;
      import java.io.*;
      import org.apache.struts.upload.*;
      public class RegPersonalDetails {
           public static String process(Regstep5Form rf) throws Exception {
                TempPhoto temp=new TempPhoto();
                Session session=null;
                Transaction tx=null;
                     session = HibernateUtil.getSession();
                     tx = session.beginTransaction();
                     String uname=rf.getUserId();
                     FormFile myFile=rf.getFileUpload();
                     byte[] filedata=myFile.getFileData();
                }catch(Exception ex){ }
                     return "success";

      --Here TempPhoto is the pojo class i.e hibernate generated class for the table in which we have to insert image...
      --You will understand this if u have idea of hibernate..
      --There may some import as statements missing...try it and configure your "struts-config.xml" file  with the above files.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
        • 1. Re: Image uploading using struts and hibernate...
          Err.. I couldn't find a question in the post. Or you just wanted to share some code? Very nice. :-)
          • 2. Re: Image uploading using struts and hibernate...
            hi to the thread starter, i am currently doing a image uploaded using struts with hibernation. I came across this thread and i would like to know, how do i implement the above to my exisiting insert page. My codes are as follow:

            A scale down version of my jsp insert page
            <html:form action="/insertBlade">
                 <TABLE border="0">

                                <TD height="57">ID:</TD>
                                <TD height="57"><html:text property="bladeid"></html:text></TD>
                                <TD height="45">Number
                                Of Blade:</TD>
                                <TD height="45"><html:text property="numofblade"></html:text></TD>
                                <TD height="54">Advance
                                <TD height="54"><html:text property="advanced_interconnect"></html:text></TD>




            Here is my Action Class:
            package project1.actions;

            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;

            import net.sf.hibernate.HibernateException;
            import net.sf.hibernate.Session;
            import net.sf.hibernate.SessionFactory;
            import net.sf.hibernate.cfg.Configuration;

            import org.apache.struts.action.Action;
            import org.apache.struts.action.ActionForm;
            import org.apache.struts.action.ActionForward;
            import org.apache.struts.action.ActionMapping;
            import org.apache.struts.action.DynaActionForm;

            import project1.forms.insertbladeFormBean;

            * @version      1.0
            * @author
            public class InsertBladeActionClass extends Action {

                 * Read value submitted by user and insert a record in CONTACT table.
                 public ActionForward execute(
                      ActionMapping mapping,
                      ActionForm form,
                      HttpServletRequest request,
                      HttpServletResponse response)
                      throws Exception {
                      Session session = null;
                      try {
                           // Get instance of contactForm for reading values submitted through form
                           DynaActionForm insertBladeForm = (DynaActionForm)form;
                           // This step will read hibernate.cfg.xml and prepare hibernate for use
                           SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

                           session =sessionFactory.openSession();
                           //Create new instance of Contact and set values in it by reading them from form object
                           insertbladeFormBean insertbladebean = new insertbladeFormBean();
                      } catch (HibernateException e) {
                           //If some thing goes wrong show failure page to user
                           return mapping.findForward("failure");
                           // Actual contact insertion will happen at this step
                           System.out.println("COMMIT HOO~~~~~~~****************************");


                      // If everything goes as per plan show contact details to user.
                      return mapping.findForward("sucess");


            Here is my Bean file:
            package project1.forms;

            import javax.servlet.http.HttpServletRequest;
            import org.apache.struts.action.ActionErrors;
            import org.apache.struts.action.ActionForm;
            import org.apache.struts.action.ActionMapping;

            * Form bean for a Struts application.
            * Users may access 3 fields on this form:
            * <ul>
            * <li>address - [your comment here]
            * <li>name - [your comment here]
            * <li>city - [your comment here]
            * </ul>
            * @version      1.0
            * @author
            public class insertbladeFormBean extends ActionForm {

                 private String bladeid;
                 private String numofblade;
                 private String advanced_interconnect;
                 private long id;

                 * Get address
                 * @return String
                 public String getBladeid() {
                      return bladeid;

                 * Set address
                 * @param <code>String</code>
                 public void setBladeid(String string) {
                      this.bladeid = string;

                 * Get name
                 * @return String
                 public String getNumofblade() {
                      return numofblade;

                 * Set name
                 * @param <code>String</code>
                 public void setNumofblade(String string) {
                      this.numofblade = string;

                 * Get city
                 * @return String
                 public String getAdvanced_interconnect() {
                      return advanced_interconnect;

                 * Set city
                 * @param <code>String</code>
                 public void setAdvanced_interconnect(String string) {
                      this.advanced_interconnect = string;
                 public long getId() {
                      return id;

                 * @param l
                 public void setId(long l) {
                      id = l;

            Finally here is my xml file:
            <?xml version="1.0"?>
            <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

                 <class name="project1.forms.insertbladeFormBean" table="BM_BLADECHASSIS">
                      <id name="id" type="long" column="ID" >
                           <generator class="seqhilo">     
                                <param name="sequence">insert_blade_id_seq</param>
                                <param name="max_lo">100</param>
                      <!-- A cat has to have a name, but it shouldn' be too long. -->
                      <property name="bladeid">
                           <column name="BLADE_ID" />
                      <property name="numofblade">
                           <column name="NUM_OF_BLADE"/>
                      <property name="advanced_interconnect">
                           <column name="ADVANCED_INTERCONNECT"/>


            please teach me what changes must i made or add so as to insert image.
            • 3. Re: Image uploading using struts and hibernate...
              can u please include the phototemp file?? please i need it
              • 4. Re: Image uploading using struts and hibernate...
                can any1 please reply?.... is the thread starter's code valid?? do i need a mapping xml file for it? and the did the thread starter miss out a file? that Tempphoto file?
                • 5. Re: Image uploading using struts and hibernate...
                  please send me hibernate config file with pojo file
                  • 6. Re: Image uploading using struts and hibernate...
                    what is data type of image in mysql?plz say anyone....