使用commons fileupload在表中存储图像

时间:2014-02-14 06:20:34

标签: hibernate apache-commons-fileupload

我在使用commons fileupload存储图像时遇到问题。当我试图更新我的表时,它给我null。该怎么办 ? 我的代码在这里......

<%-- 
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>

我有会话变量,但图像字段没有更新,我得到了null。怎么办???

0 个答案:

没有答案