Storing image in table using commons fileupload

♀尐吖头ヾ 提交于 2019-12-25 12:09:56

问题


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

来源:https://stackoverflow.com/questions/21772181/storing-image-in-table-using-commons-fileupload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!