The point to be emphasized here is:
The input stream and the output stream, the difference is the out and in, is relative to the program. The input stream gets the data, and the output stream is the output data. Input is the program to get external data, the output is the program to the external transmission of data, there is no necessary connection between the two, are the flow, the difference is the direction of different, that is, the program can only input stream and no output stream, or only the output stream and no input stream.
The pseudo code is as follows:
OutputStream oos = null; //
InputStream IIS = NULL; Read-in from local
IIS = new Bufferedinputstream (new FileInputStream (new file));//Read the picture data through the input stream, where filename represents the file path
Byte[] bs = new byte[1024];
int length =-1;
while (length = Iis.read (BS, 0, bs.length))! =-1) {///If it is output, this is the write function.
Oos.write (BS, 0, length);
Oos.flush ();
}
But because the developer's system is different and the computer disk location is different
So here's a common Java system attribute that can be used
String s=system.getproperty ("User.home"); Get the appropriate file location
Here is a description of the storage of the pictures in the database:
Public voidSavecommodity (list<string> Sqls, list<list<object>>params,string CID, String filename)throwsSQLException {dbhelper db=NewDBHelper ();//cphoto This column is isolated from the new number and inserted in a streamString sql2= "Select photo from Tb_record where id=? For Update ";//String sql1 = "Update Tb_record set photo=?" where record_number=? ";//Connection con =NULL;Try{con=Db.getcon (); Con.setautocommit (false); PreparedStatement pstmt=NULL; for(inti = 0; I < sqls.size (); i++) {String SQL=Sqls.get (i);p stmt=con.preparestatement (SQL); List<Object> param =Params.get (i); for(intj = 0; J < Param.size (); J + +) {Pstmt.setobject (J+ 1, Param.get (j));} Pstmt.executeupdate ();}if(FileName! =NULL&&!filename.equals ("") ) {pstmt=con.preparestatement (SQL2);p stmt.setstring (1, CID); ResultSet RS=pstmt.executequery ();if(rs! =NULL&&Rs.next ()) {OutputStream Oos=NULL;//Enter a picture into the databaseInputStream IIS =NULL;//read-in from localOracle.sql.BLOB B = (Oracle.sql.BLOB) Rs.getblob (1); Oos= B.getbinaryoutputstream ();//Remove the input stream from the result set for data to the databaseIIS =NewBufferedinputstream (NewFileInputStream (NewFile (filename));//reading picture data from an input streambyte[] bs =New byte[1024];intLength =-1; while(length = Iis.read (BS, 0, bs.length))! =-1) {oos.write (BS,0, length); Oos.flush ();}}} Con.commit ();} Catch(Exception e) {e.printstacktrace (); Con.rollback ();}finally{con.setautocommit (true); Con.close ();}}
View Code
about the input stream and the output stream