前台页面图片无法显示
通过使用mybatis的xml配置文件来进行数据库查询
xml中的图片查询语句:
<select id="selectPaintingImg" resultType="com.imooc.gallery.entity.Oil_painting"> select `smallImg` from `oil-painting` where id = #{id} </select>
DAO中获取图片的方法:
public static Oil_painting getPaintingImg(Long id) { Oil_painting painting = null; try { sqlSession = MybatisUtils.getSqlSession(); painting = sqlSession.selectOne("selectPaintingImg",new Oil_painting(id)); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return painting; }
service中代码:
if ("/getImg.jpg".equals(request.getServletPath())){ //获取油画的图片id String idStr = request.getParameter("id"); //获得指定id的油画信息 Oil_painting painting = HomeService.getPaintingImg(Long.valueOf(idStr)); InputStream input = null; ServletOutputStream out = null; try { response.setContentType("multipart/form-data"); if (null != painting && null != painting.getSmallImg()){ System.out.println(painting.getSmallImg().length); input = new ByteArrayInputStream(painting.getSmallImg()); out = response.getOutputStream(); byte[] b = new byte[1024]; int length = input.read(b); while(length != -1){ out.write(b); length = input.read(b); } out.flush(); response.flushBuffer(); } } catch (IOException e) { e.printStackTrace(); } finally { out.close(); input.close(); } }
某些时候可以加载所有图片:
当刷新页面后有些图片就无法显示:
根据调试的结果显示是没有从数据库中获得相应id的油画对象而抛出的空指针异常。对DAO层的查询方法调试的时候当执行查询语句时抛出异常:Executor was closed
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NullPointerException ### The error may exist in mapper/PaintingMapper.xml ### The error may involve com.imooc.gallery.entity.Oil_painting.selectPaintingImg ### The error occurred while executing a query ### Cause: java.lang.NullPointerException
11
收起
正在回答 回答被采纳积分+1
1回答
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星