问题
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