lorcx 2016-01-13 13:50 采纳率: 46.7%
浏览 2937
已采纳

jsp 输出图片 blob.getBytes()问题

    response.setContentType("image/jpeg");
String userName = request.getParameter("userName");
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
Connection conn = DriverManager.getConnection(url,"llxx","lorcx");
StringBuilder sql = new StringBuilder();
sql.append("select u.pic from base_user u where u.user_name = '").append(userName).append("'");
PreparedStatement ps = conn.prepareStatement(sql.toString());
ResultSet rs = ps.executeQuery();
if(rs != null){
  while (rs.next()){
    Blob blob = rs.getBlob(1);//获取文件
    int size = (int)blob.length();
    byte[] b = blob.getBytes(1,size);
    ServletOutputStream os = response.getOutputStream();
    os.write(b);
    os.flush();
    os.close();
  }
}

    我在调用getBytes时开始位置是1才能显示,为什么不是0
  • 写回答

2条回答 默认 最新

  • 毕小宝 博客专家认证 2016-01-13 23:43
    关注

    java.util.sql.Blog类型的getBytes方法,第一个参数pos是指从第几个字节开始提取数据,这个是实际的字节序号。
    显然第0个字节是没有东西的,第一个字节的位置是1的。可以参考jdk API是这样的:

        @param pos the ordinal position of the first byte in the
        <code>BLOB</code> value to be extracted; the first byte is at position 1
    

    直接用getBytes,需要自己计算起止位置,还有一个方法可以直接获取二进制文件流对象的。

     InputStream bis = blob.getBinaryStream() ; //得到Blob实例的字节流 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗