Package Com.ronniewang.downloadpicture;import Java.io.datainputstream;import Java.io.file;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.net.malformedurlexception;import Java.net.URL; Import Java.sql.resultset;import java.sql.sqlexception;import Java.util.arraylist;import com.mysql.jdbc.Connection ; Import Com.mysql.jdbc.statement;import Com.ronniewang.utilities.jdbcutil;public class Downloadpicture {public static void Main (string[] args) {downloadpicture downloadpicture = new Downloadpicture (); arraylist<string> urllist = downloadpicture.readurllist ();d ownloadpicture.downloadpicture (urlList);} /** * The URL list of the image to be downloaded, download the corresponding image of the URL to local * @param urllist */private void Downloadpicture (arraylist<string> urllist) { URL url = null;int imagenumber = 0;for (String urlstring:urllist) {try {url = new URL (urlstring);D Atainputstream DATAINP Utstream = new DataInputStream (Url.openstream ()); String imageName = Imagenumber + ". jpg"; FileOutputStream FileOutputStream = new FilEoutputstream (New File (ImageName)); byte[] buffer = new Byte[1024];int length;while (length = Datainputstream.read ( Buffer)) > 0) {fileoutputstream.write (buffer, 0, length);} Datainputstream.close (); Fileoutputstream.close (); imagenumber++;} catch (Malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}} /** * Connect to MySQL database. Read the URL list of pictures to download by querying the database * @return */private arraylist<string> readurllist () {arraylist<string> urllist = new Arraylist<string> (); try {Connection Connection = (Connection) jdbcutil.getconnection (); Statement Statement = (Statement) connection.createstatement (); String sql = "Select URL from url"; Query statement transposition Corresponding SELECT statement ResultSet resultSet = statement.executequery (sql); while (Resultset.next ()) {String URL = resultset.getstring ("url"); Urllist.add (URL); System.out.println (URL);} Jdbcutil.free (ResultSet, statement, connection);} catch (SQLException e) {e.printstacktrace ();} return urllist;}}
Jdbcutil class codes such as the following:
Package Com.ronniewang.utilities;import java.sql.*; Import javax.sql.*; /** * Tool class to connect MySQL database. Auxiliary class used as a database connection for downloadpicture * @author Administrator * */public Final class Jdbcutil {//The following configuration can be replaced with the corresponding content to be private Stati c String url = "Jdbc:mysql://localhost:3306/test"; private static String user = "root"; private static String password = "123456"; Private Jdbcutil () {} static {try {Class.forName ("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) {throw new Exceptionininitializererror (e); }} public static Connection getconnection () throws sqlexception{return Drivermanager.getconnection ( URL, user, password); } public static void Free (ResultSet rs, Statement St, Connection conn) {try {if (rs! = NULL) {rs.close (); }} catch (SQLException e) {e.printstacktrace (); } finally { try {if (st! = null) {st.close (); }} catch (SQLException e) {e.printstacktrace (); } finally {if (conn! = null) {try {conn.close (); } catch (SQLException e) {e.printstacktrace (); } } } } } }
Test database table field ID and URL two
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
java download pictures by URL