小弟经验不足,在实现一个图片上传的功能的时候是这样代码,但是初始化用户列表的时候页面上一直空指针,而控制台则是下面的异常,非常头疼,请各位大侠帮小弟解决下困难。谢谢!
以下为相关代码:
// 添加用户
public String addUsers() throws Exception {
String rs = null;
此处为上传图片的代码
                         if (f != null) { FileInputStream f = new FileInputStream(getF());
byte[] data = new byte[(int) f.available()];
BufferedInputStream bis = new BufferedInputStream(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos); int k = 0;
while ((k = bis.read()) != -1) {
bos.write(k);
}
bos.flush(); // 提交文件流,很关键
bis.close();
user.setUserPic(baos.toByteArray()); System.out.println("图片:" + user.getUserPic());
System.out.println("姓名:" + user.getUserName());
}

boolean bool = userManageBiz.addUsers(user);
if (bool) {
rs = SUCCESS;
} else {
rs = ERROR;
}
return rs;
}
if (user.getUserPic() != null) {
// 得到项目路径
String dirPath = System.getProperty("user.dir");
String s = dirPath.replace("bin", "");
// 用来存储照片的文件路径(完整路径)
String picPath = s + "webapps\\Dcu32X_BS\\avatar\\"
+ user.getId() + ".jpg";
// 创建目录
// CreateFileUtil.createDir(dirPath);
// 创建文件
CreateFileUtil.CreateFile(picPath);
// 远程访问路径
user.setPicPath("/avatar/" + user.getId() + ".jpg");
// Blob photo = user.getUserPic();
// InputStream in = null;
try {
File file = new File(user.getPicPath()); FileOutputStream outfile = new FileOutputStream(file);
try {
outfile.write(user.getUserPic(), 0,
user.getUserPic().length);
outfile.flush();
outfile.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 得到部门下的人员列表
public String findDeptUsers() throws Exception {
String rs = null;
Page page = user.getPage();
if (page.getCurrPage() < 0) {
page.setCurrPage(1);
}
if (request.getParameter("id") != null) {
user.setDeptId(Short.parseShort(request.getParameter("id")));
}
List list = userManageBiz.findUsersListByDeptId(user);
request.setAttribute("page", page);
request.setAttribute("userList", list);
rs = SUCCESS;
return rs;
}
public List findUserList(final Users user) {
        List list = null;
        try {
            final Page page = user.getPage();
            list = getHibernateTemplate().executeFind(new HibernateCallback() {                public Object doInHibernate(Session s)
                        throws HibernateException, SQLException {
                    Criteria c = s.createCriteria(Users.class);
                    if (user != null) {
                     //部门
                        if (user.getDeptId() != null && user.getDeptId()>0) {
     c.add(Restrictions.eq("deptId",user.getDeptId()));
     }
c.addOrder(Order.desc("id"));
//分页
////总数据条数
page.setTotalCount(c.list().size());
////总页数的第几页
c.setFirstResult((page.getCurrPage() -1) * page.getCount());
////每页的数据条数
c.setMaxResults(page.getCount());
}
return c.list();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
数据库中userPic的类型为image,实体类中类型为byte[],users.hbm.xml中的类型为        <property name="userPic" type="org.springframework.orm.hibernate3.support.BlobByteArrayType">
            <column name="UserPic" />
        </property>
红色代码部门是出现异常的地方,异常信息为
[code=Java]java.lang.IllegalStateException: No LobHandler found for configuration - lobHandler property must be set on LocalSessionFactoryBean
at org.springframework.orm.hibernate3.support.AbstractLobType.nullSafeGet(AbstractLobType.java:152)
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:132)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2267)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1424)
code]

解决方案 »

  1.   

     public List findUserList(final Users user) {
            List list = null;
            try {
                final Page page = user.getPage();
                list = getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session s)
                            throws HibernateException, SQLException {
                        Criteria c = s.createCriteria(Users.class);
                        if (user != null) {
                            //部门
                               if (user.getDeptId() != null && user.getDeptId()>0) {
                                c.add(Restrictions.eq("deptId",user.getDeptId()));
                            }
                            c.addOrder(Order.desc("id"));
                            //分页
                            ////总数据条数
                            page.setTotalCount(c.list().size());
                            ////总页数的第几页
                            c.setFirstResult((page.getCurrPage() -1) * page.getCount());
                            ////每页的数据条数
                            c.setMaxResults(page.getCount());
                        }
                        return c.list();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list;
        }