0

I have a problem in storing image using commons fileupload. It gives me null when i am trying to update my table . What to do ? My code is here...

<%-- 
Document   : image_process
Created on : Feb 1, 2014, 2:42:53 PM
Author     : parag
--%>

<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page import="Pojos.hiber"%>
<%@page import="Pojos.Users"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.hibernate.Transaction"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <%


      request.getSession(true);
      String username = (String) session.getAttribute("un");
      out.println(username);
      byte[] b=null;

   try{

  SessionFactory sessionFactory = hiber.getSessionFactory();
  out.println("Ok");
  Session s = sessionFactory.openSession();

  Users us = new Users();
  DiskFileItemFactory factory = new DiskFileItemFactory(); 
  System.out.println("ok");
  ServletFileUpload sfu = new ServletFileUpload(factory);
  List items = sfu.parseRequest(request);      
  Iterator iter = items.iterator();



  while (iter.hasNext()) {
      FileItem item = (FileItem) iter.next();
              if (!item.isFormField()) {
                               b = item.get();
                                        }
           }


  //String u = (String) us.getUserName();
  out.println(us.getUserName());
  if(username.equals(us.getUserName()))
  {
  us.setProfPic(b);
  }
  s.save(us);

Transaction  t = s.beginTransaction();

  t.commit();

  System.out.println("ok");
  s.close();
 // response.sendRedirect("index.jsp");

    }catch(Exception e){

        System.out.println("Error:::: "+e.getMessage());

    }








    %>
</body>

I am got the session variable , but the image field is not updated and i got null. What to do???

sadman
  • 11
  • 3
  • First question I have is> Are you sure you are receiving the bytes in your jsp code _, try to do b.size and be sure is not null and different from zero – Koitoer Feb 14 '14 at 06:29
  • Second advice make this Transaction t = s.beginTransaction(); then s.save(us) and then t.commit(); in that order – Koitoer Feb 14 '14 at 06:32
  • Yes, I have tried that . Even i have used it in my project. But the problem is that i am not getting it now means when i was using the traditional database queries at that time it worked well, but when i have used hibernate, i am getting null. – sadman Feb 14 '14 at 06:35
  • So I assume private byte[] profPic; is the field and you use @Lob annotation over the field, as jpa need it to determine you will use a BLOB, consider read this man http://howtodoinjava.com/2013/08/30/hibernate-example-of-insertselect-blob-from-database/, it is a really good example of usage blob in hibernate – Koitoer Feb 14 '14 at 06:37
  • I am developing my project in netbeans and i am using reverse engineering file and table mapping file in this . But i am not getting it. – sadman Feb 14 '14 at 06:41

0 Answers0