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

解决方案 »

  1.   

    你是读取文件并转为byte[]数组不熟悉,还是保存数据库不熟悉?
      

  2.   

     
    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
    不过PreparedStatement对象好像有方法直接以inputstream为对象写入的方法,你可以试试,本人没有用过