title: convert file or byte[] into BLOB in JAVA
body: <p>I'm sending a file frome a client to a server and receive it like this: 
  </p>
  
  <pre class="lang-java prettyprint-override"><code>            //Receive File:
              FileOutputStream fis = new FileOutputStream("receivedTest");
              DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
              int count;
              byte[] buffer = new byte[4096];
              while ((count = dis.read(buffer)) &gt; 0)
              {
                fis.write(buffer, 0, count);
              }
              fis.close();
  </code></pre>
  
  <p>Just like it's explained in <a href="https://stackoverflow.com/questions/9520911/java-sending-and-receiving-file-byte-over-sockets/">this subject</a>.
  It's working well. But the fact is, i don't really want to receive the file itself; i want a BLOB. I've read that a BLOB is just like a byte[]. </p>
  
  <p>In my database class (i use SQLite), i have the following table: </p>
  
  <pre class="lang-java prettyprint-override"><code>    String sqlFile = "CREATE TABLE IF NOT EXISTS files (\n"
              + " id integer PRIMARY KEY,\n"
              + " shorthash byte[],\n"
              + " filename text NOT NULL,\n"
              + " file blob,\n"
              + " owner text\n"
              + ");";
  </code></pre>
  
  <p>and the following function to insert a new "file": </p>
  
  <pre class="lang-java prettyprint-override"><code>public void insertFile(byte[] shorthash, String filename, byte[] file, String owner) {
      String sql = "INSERT INTO files(shorthash,filename, file, owner) VALUES(?,?,?,?)";
  
      try (Connection conn = DriverManager.getConnection(url);
              PreparedStatement pstmt = conn.prepareStatement(sql)) {
          pstmt.setBytes(1, shorthash);
          pstmt.setString(2, filename);
          pstmt.setBytes(3, file);
          pstmt.setString(4, owner);
          pstmt.executeUpdate();
      } catch (SQLException e) {
          System.out.println(e.getMessage());
      }
  }
  </code></pre>
  
  <p>As you can see, there's 4 columns, the file itself is in the 3rd column. In the table it's declared as a BLOB, but when i insert it i'm just doing a setBytes. I'm not sure if this is right, it's just what i've found on internet. </p>
  
  <p>So, i'm receiving this file on my server, and i want to store it in my database. If possible, i would like to avoid creating the file on server side (the line FileOutputStream fis = new FileOutputStream("receivedTest"); in my first code). I would like to store it directly in the database, since i receive it as a byte array i think it'll be easier this way. </p>
  
  <p>But i don't know how to do that. Probably because i don't really understand the link between Blob and byte[]. I mean, a byte array may be too little to hold an entire file; but a blob is ok. however, to insert the file in database, i insert a byte array. This make nonsens to me.</p>
  
  <p>EDIT: </p>
  
  <p>So, i've tried two things: first, adding the file in the DB as it's done <a href="https://stackoverflow.com/questions/9476434/how-to-store-a-file-in-a-mysql-database-using-blob">here</a> (and pretty much everywhere i looked, it's always done that way):</p>
  
  <pre class="lang-java prettyprint-override"><code>//Receive encrypted File:
              FileOutputStream fos = new FileOutputStream("receivedTest");
              DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
              int count;
              byte[] buffer = new byte[4096];
              while ((count = dis.read(buffer)) &gt; 0)
              {
                fos.write(buffer, 0, count);
              }
              fos.close();
  
              DB.insertFile(shorthash, "test", "receivedTest", user);
  
  //Insert file in DB:
  public void insertFile(byte[] shorthash, String filename, String filepath, String owner) throws FileNotFoundException {
      String sql = "INSERT INTO files(shorthash, filename, file, owner) VALUES(?, ?, ?, ?)";
      try (Connection conn = DriverManager.getConnection(url);
              PreparedStatement pstmt = conn.prepareStatement(sql)) {
          pstmt.setBytes(1, shorthash);
          pstmt.setString(2, filename);
  
          File file = new File(filepath);
          FileInputStream   fis = new FileInputStream(file);
          pstmt.setBinaryStream(3, fis, (int) file.length());
          pstmt.execute();
          pstmt.setString(4, owner);
          pstmt.executeUpdate();
          fis.close()
      } catch (SQLException e) {
          System.out.println(e.getMessage());
      }
  }
  </code></pre>
  
  <p>Second, insert the file as a byte array (but that won't work for big files) as it's explained in <a href="http://www.sqlitetutorial.net/sqlite-java/jdbc-read-write-blob/" rel="nofollow noreferrer">SQLite Tutorial</a> :</p>
  
  <pre class="lang-java prettyprint-override"><code>//Insert file in DB:
  public void insertFile(byte[] shorthash, String filename, String filepath, String owner) throws FileNotFoundException {
      String sql = "INSERT INTO files(shorthash, filename, file, owner) VALUES(?, ?, ?, ?)";
      try (Connection conn = DriverManager.getConnection(url);
              PreparedStatement pstmt = conn.prepareStatement(sql)) {
          pstmt.setBytes(1, shorthash);
          pstmt.setString(2, filename);
  
          File file = new File(filepath);
          FileInputStream   fis = new FileInputStream(file);    
          byte[] buffer = new byte[1024];
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          for (int len; (len = fis.read(buffer)) != -1;)
              bos.write(buffer, 0, len);
          fis.close()
          pstmt.setBytes(3, bos.toByteArray());
          pstmt.execute();
          pstmt.setString(4, owner);
          pstmt.executeUpdate();
      } catch (SQLException e) {
          System.out.println(e.getMessage());
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Then, when i print my DB, there is no file in it. Same if i try to open the DB with DB Browser. The console only say: </p>
  
  <pre class="lang-java prettyprint-override"><code>Connection to SQLite has been established. 
  ouverture du server
  Clients:
  
  1   admin   admin
  
  Files:
  </code></pre>