3 Replies Latest reply on Aug 7, 2007 10:54 AM by Manu Osten

    image to blob

    Manu Osten Apprentice

      Hello,

      I have problem with Blob type.

      A Image should be uploaded then stored in tempory file. The image should be stored from tempory file into mysql over enitity bean as Blob Object.

      How can I make from image to blob object without SQL-statement?

      Enitity Bean InterpreterImage

      public class InterpreterImage implements Serializable
      {
       private static final long serialVersionUID = 1L;
      
       @Id
       @Column(name="ID")
       private int id;
      
       @Column(name="GALLERYFILENAME")
       private String galleryFilename;
      
       @Column(name="GALLERYIMAGE")
       private Blob galleryImage;
      
       @Column(name="PROFILFILENAME")
       private String profilFilename;
      
       @Column(name="PROFILIMAGE")
       private Blob profilImage;
      


      Store Blob Object into entity Bean Interpreter.interpreterImage
      ......
      interpreter.getInterpreterImage().setProfilImage(
       setImagetoPath(profilFile.getInputStream(), filename));
      .....
      


      From Image to Blob Object. But how???
       private Blob setImagetoPath(InputStream input, String Filename) {
      
       FileOutputStream fos;
       Blob blob = null;
       String realPath=null;
       byte[] buff = new byte[8192];
       int len = 0;
      
       try {
       System.out.println(Filename);
       realPath = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/images");
       fos = new FileOutputStream(realPath+"/"+Filename);
       while (0 < (len = input.read(buff))){
       fos.write(buff, 0, len);
       }
       System.out.println(fos.toString());
       fos.close();
       input.close();
       } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       try {
       //Image over byte[] buff to Blob Object
       // HOW????
       } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       return blob;
       }
      


      Store simple Bean into database
       public void createInterpreter(Interpreter interpreter) {
       manager.persist(interpreter);
       }
      

      I hope, that you have an idea... Thank you in advance

      Manu


        • 1. Re: image to blob
          J-C jc Apprentice

          @Lob byte[] image;

          • 2. Re: image to blob
            Manu Osten Apprentice

            Wow. That is really simple way. How do you know? I don't find it in internet.
            So I have changed from Blob to byte[], but I got error message.

            Entity InterpreterImage

            @Entity
            @Table(name="INTERPRETERIMAGE")
            public class InterpreterImage implements Serializable
            {
             ....
             .....
             @Lob
             @Column(name="GALLERYIMAGE")
             private byte[] galleryImage;
             .....
            
             public String getGalleryFilename() {
             return galleryFilename;
             }
            
             public void setGalleryFilename(String galleryFilename) {
             this.galleryFilename = galleryFilename;
             }
            
             public byte[] getGalleryImage() {
             return galleryImage;
             }
            
             public void setGalleryImage(byte[] galleryImage) {
             this.galleryImage = galleryImage;
             }
            


             public String submitMyProfil() {
             interpreter.getInterpreterImage().setGalleryImage(galleryFile.getBytes());
             interpreter.getInterpreterImage().setGalleryFilename(filename);
            }
            


            Error message:
            13:22:10,675 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
            javax.faces.FacesException: Error calling action method of component with id _idJsp0:_idJsp24
             at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
             at javax.faces.component.UICommand.broadcast(UICommand.java:109)
             at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
             at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
             at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
             at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
             at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
             at javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
             at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:147)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
             at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
             at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
             at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
             at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
             at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
             at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
             at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
             at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
             at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
             at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
             at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
             at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
             at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
             at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
             at java.lang.Thread.run(Thread.java:619)
            Caused by: javax.faces.el.EvaluationException: Exception while invoking expression #{interpreterDataImageHandler.submitMyProfil}
             at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:156)
             at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:61)
             ... 29 more
            Caused by: java.lang.NoSuchMethodError: de.kirchedlau.ponte.ejb.entities.InterpreterImage.setGalleryImage([B)V
             at de.kirchedlau.ponte.InterpreterDataImageHandler.submitMyProfil(InterpreterDataImageHandler.java:44)
             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 org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:132)
             ... 30 more
            


            • 3. Re: image to blob
              Manu Osten Apprentice

              Sorry, I found a supid error. Now I solved my problem. Thankx

              Manu