日期:2014-05-19  浏览次数:20657 次

将本地文件(图片等)转换成byte【】保存到数据库
根据本地文件路径 将文件(图片等)转换成byte[]保存到数据库中 大侠帮忙

------解决方案--------------------
Java code

InputStream is = 类名.class.getResourceAsStream("/xxx.yyy");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int h;
while((h = is.read(b, 0, b.length)) != -1) {
      baos.write(b, 0, h);
}
//   链接数据库的代码省略..............
PreparedSteatement pstmt = conn.preparedStatement("insert into tablename (xxx) values (?)");
pstmt.setBytes(1, baos.toByteArray());
pstmt.executeUpdate();
.........................
pstmt.set