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
jsp 输出图片 blob.getBytes()问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注 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实例的字节流
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报