Java根据web网站Url下载图片

Java根据web网站Url下载图片

 

 

  1. package com.ronniewang.downloadpicture;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.net.MalformedURLException;  
  8. import java.net.URL;  
  9. import java.sql.ResultSet;  
  10. import java.sql.SQLException;  
  11. import java.util.ArrayList;  
  12.   
  13. import com.mysql.jdbc.Connection;  
  14. import com.mysql.jdbc.Statement;  
  15. import com.ronniewang.utilities.JdbcUtil;  
  16.   
  17. public class DownloadPicture {  
  18.   
  19.     public static void main(String[] args) {  
  20.        测试下载图片:
  21.        http://img.iitu.net/img_lt/58pic/12/50/47/10U58PICMxQ.jpg
  22.        http://img.iitu.net/img_lt/58pic/12/50/47/02C58PIC5hd.jpg
  23.        http://img.iitu.net/img_lt/58pic/12/50/46/92E58PICZM4.jpg
  24.         DownloadPicture downloadPicture = new DownloadPicture();  
  25.         ArrayList<String> urlList = downloadPicture.readUrlList();  
  26.         downloadPicture.downloadPicture(urlList);  
  27.     }  
  28.   
  29.     /** 
  30.      * 传入要下载的图片的url列表,将url所对应的图片下载到本地 
  31.      * @param urlList 
  32.      */  
  33.     private void downloadPicture(ArrayList<String> urlList) {  
  34.         URL url = null;  
  35.         int imageNumber = 0;  
  36.           
  37.         for (String urlString : urlList) {  
  38.             try {  
  39.                 url = new URL(urlString);  
  40.                 DataInputStream dataInputStream = new DataInputStream(url.openStream());  
  41.                 String imageName = imageNumber + ".jpg";  
  42.                 FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));  
  43.   
  44.                 byte[] buffer = new byte[1024];  
  45.                 int length;  
  46.   
  47.                 while ((length = dataInputStream.read(buffer)) > 0) {  
  48.                     fileOutputStream.write(buffer, 0, length);  
  49.                 }  
  50.   
  51.                 dataInputStream.close();  
  52.                 fileOutputStream.close();  
  53.                 imageNumber++;  
  54.             } catch (MalformedURLException e) {  
  55.                 e.printStackTrace();  
  56.             } catch (IOException e) {  
  57.                 e.printStackTrace();  
  58.             }  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * 连接mysql数据库,通过查询数据库读取要下载的图片的url列表 
  64.      * @return 
  65.      */  
  66.     private ArrayList<String> readUrlList() {  
  67.         ArrayList<String> urlList = new ArrayList<String>();  
  68.         try {  
  69.             Connection connection = (Connection) JdbcUtil.getConnection();  
  70.             Statement statement = (Statement) connection.createStatement();  
  71.             String sql = "select url from url"//查询语句换位相应select语句  
  72.             ResultSet resultSet = statement.executeQuery(sql);  
  73.               
  74.             while (resultSet.next()) {  
  75.                 String url = resultSet.getString("url");  
  76.                 urlList.add(url);  
  77.                 System.out.println(url);  
  78.             }  
  79.               
  80.             JdbcUtil.free(resultSet, statement, connection);  
  81.         } catch (SQLException e) {  
  82.             e.printStackTrace();  
  83.         }  
  84.   
  85.         return urlList;  
  86.     }  
  87.   
  88. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值