-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: How to store whole image to database (not just a path) ????
PostPosted: Wed Apr 15, 2009 5:10 pm 
Newbie

Joined: Thu Mar 19, 2009 6:30 pm
Posts: 7
As in the topic. When I try to save java.io.File in database using Hibernate, only path to this file and some small additional informations are stored. How can I save whole file? I'm using annotations.


Top
 Profile  
 
 Post subject: Convert it to a blob first
PostPosted: Wed Apr 15, 2009 10:28 pm 
Newbie

Joined: Tue Apr 07, 2009 6:02 pm
Posts: 6
convert it to a blob.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 1:20 am 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
What he said: get a byte[] from the file and save() that as a blob.

Code:
FileInputStream fio = new FileInputStream(file);
byte[] bytes = new byte[fio.available()];
fio.read(bytes);
fio.close();
record.setContent(bytes);  // set 'content' column
save(record);


With appropriate exception handling...

http://java.sun.com/javase/6/docs/api/j ... tream.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 3:14 am 
Newbie

Joined: Thu Mar 19, 2009 6:30 pm
Posts: 7
Thank you for answering. I was trying to save file as blob earlier, but I use a4j:mediaOutput to generate images dynamically from database and when calling paint method I was getting something like "bytes cannot be get from blob outside of transaction" (I don't remember exactly). Anyway, even if there would be no exception mediaOutput cannot display large data as data is encoded in URL. When I was saving images as files on disk, mediaOutput worked fine, but I would like to avoid saving files on disk.

Do you know any other way of dynamic generating image stored as blob???


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 7:44 am 
Newbie

Joined: Wed Jan 17, 2007 9:39 pm
Posts: 5
You can create a servlet that renders the image from database.
Here's the code:

Code:
public class ViewImageServlet extends HttpServlet {

   @Override
   protected void doGet(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      try {
         byte[] img = null;
         if (request.getParameter("id") != null) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            DAO dao = new DAO();
            MyOBJ obj = dao.getObj(id);
            img = obj.getImage();
         }
         if (img == null || img.length == 0) {
            String fileName = getServletContext().getRealPath(
                  "/noimage.gif");
            File file = new File(fileName);
            FileInputStream in = new FileInputStream(file);
            img = new byte[(int) file.length()];
            in.read(img);
            in.close();
         }
         response.setContentLength(img.length);
         response.setContentType("image/jpeg");
         OutputStream out = response.getOutputStream();
         out.write(img);
         out.flush();
         out.close();
      } catch (DAOException e) {
         e.printStackTrace();
      }
   }

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      doGet(req, resp);
   }
}


When you wanna render it to browser you simply call or put in src attribute from image the following address (for example): http://localhost:8080/myproject/viewImage?id=1


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 8:57 am 
Newbie

Joined: Thu Mar 19, 2009 6:30 pm
Posts: 7
Thanks ACDias!


Top
 Profile  
 
 Post subject: Re: How to store whole image to database (not just a path) ????
PostPosted: Tue Mar 08, 2011 1:46 am 
Newbie

Joined: Thu Feb 10, 2011 3:06 am
Posts: 2
can anyone give me full code to upload image using hibernate and JSP
Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.