java - Getting image from server and saving to MySQL DB -
im getting image server inputstream , saving mysql database. works when utilize thread.sleep(5000);
. if dont utilize no image saved db or 1 image , half of or less. understand programme needs time writing image database, how much time? question, know when finished writing image database , can start next image. below code:
resultset rs = stmt.executequery(query); while (rs.next()) { int id = rs.getint(1); string myname = rs.getstring(2); seek { string mycommand = "take image , save /mydir/mydir2/mydir3" + myname + ".png"; telnet.sendcommand(mycommand); // here taking image via telnet // thread.sleep(5000);// if uncomment line works string sqlcommand = "update my_table set image = ? id ='" + id +"';"; preparedstatement statement = conn.preparestatement(sqlcommand); string ftpurl = "ftp://"+server_ip+"/mydir/mydir2/mydir3" + myname + ".png;type=i"; url url = new url(ftpurl); urlconnection connurl = url.openconnection(); //thread.sleep(5000); // if uncomment line, works too. inputstream inputstreamtelnet = connurl.getinputstream(); statement.setblob(1, inputstreamtelnet); int row = statement.executeupdate(); if (row > 0) { system.out.println("a image inserted db."); system.out.println("value of row(s) : " + row); } } grab (exception e) { e.printstacktrace(); } } // end of while
i expect set waiting(sleep) after inputstream inputstreamtelnet = connurl.getinputstream();
doesnt work when set sleep after line. works when sleep before. explain me why , avoid using thread.sleep(5000);
, instead wait exact time or not wait @ create programme faster there might case saving image can take more 5 seconds or maybe saving image doesnt take time opening url connection. there 2 sleep lines on code when uncomment 1 of them programme works(saves images mysql db successfully). verified on server images exist in end dont see them in mysql db.
update : removed seek block , telnet stuff works without waiting need telnet stuff...
update 2: after inspecting telnet class found out forgot apply alter made single line... works without wait!
huh, tested code on jdk 1.7.0_67 / postgresql 9.2 , works well:
public class imageloader { private static final int start_image_id = 1; private static final int end_image_id = 1000; private static final string image_url = "http://savepic.net/%d.jpg"; public static void main(string[] args) throws sqlexception, ioexception { connection connection = drivermanager.getconnection("jdbc:postgresql://localhost:5432/test", "username", "password"); preparedstatement imagestatement = connection.preparestatement("insert public.image values(?, ?)"); (int = start_image_id; <= end_image_id; i++) { string imageurl = string.format(image_url, i); url url = new url(imageurl); urlconnection urlconnection = url.openconnection(); imagestatement.setlong(1, i); imagestatement.setbytes(2, read(urlconnection.getinputstream())); int count = imagestatement.executeupdate(); if (count != 1) { throw new illegalstateexception("image id = " + + " not inserted"); } else { system.out.println("image (" + imageurl + ") saved database"); } } imagestatement.close(); connection.close(); } private static byte[] read(inputstream inputstream) throws ioexception { bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream(1 << 15); // assume image average size ~ 32 kbytes bufferedinputstream bufferedinputstream = new bufferedinputstream(inputstream); byte[] buffer = new byte[1 << 10]; int read = -1; while ((read = bufferedinputstream.read(buffer)) != -1) { bytearrayoutputstream.write(buffer, 0, read); } homecoming bytearrayoutputstream.tobytearray(); } }
java mysql url ftp inputstream
No comments:
Post a Comment