使用java下载http形式的图片

来源:互联网 发布:人变老的软件 编辑:程序博客网 时间:2024/06/08 07:30

文章来源:http://blog.csdn.net/keepfriend/article/details/53556426

用于下载http形式的图片

以下是自定义的DownUtil类:

[java] view plain copy
  1. import java.io.*;  
  2. import java.net.*;  
  3.   
  4. public class DownUtil {  
  5.   
  6.     private String path;  
  7.     private String targetFile;  
  8.     private int threadNum;  
  9.     private DownThread[] threads;  
  10.     private int fileSize;  
  11.     private class DownThread extends Thread{  
  12.         private int startPos;  
  13.         private int currentPartSize;  
  14.         private RandomAccessFile currentPart;  
  15.         public int length;  
  16.         public DownThread(int startPos, int currentPartSize, RandomAccessFile currentPart){  
  17.             this.startPos = startPos;  
  18.             this.currentPartSize = currentPartSize;  
  19.             this.currentPart = currentPart;  
  20.         }  
  21.         public void run(){  
  22.             try{  
  23.                 URL url = new URL(path);  
  24.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  25.                 conn.setConnectTimeout(5 * 1000);  
  26.                 conn.setRequestMethod("GET");  
  27.                 conn.setRequestProperty(  
  28.                     "Accept""image/gif, image/jpeg, image/pjpeg, image/pjpeg, "  
  29.                     + "application/x-shockwave-flash, application/xaml+xml, "  
  30.                     + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "  
  31.                     + "application/x-ms-application, application/vnd.ms-excel, "  
  32.                     + "application/vnd.ms-powerpoint, application/msword, */*");  
  33.                 conn.setRequestProperty("Accept-Language""zh-CN");  
  34.                 conn.setRequestProperty("Charset""UTF-8");  
  35.                 InputStream inStream = conn.getInputStream();  
  36.                 inStream.skip(this.startPos);  
  37.                 byte[] buffer = new byte[1024];  
  38.                 int hasRead = 0;  
  39.                 // Read network data and write to a local file  
  40.                 while(length < currentPartSize && (hasRead = inStream.read(buffer)) !=  -1){  
  41.                     currentPart.write(buffer, 0, hasRead);  
  42.                     length += hasRead;  
  43.                 }  
  44.                 currentPart.close();  
  45.                 inStream.close();  
  46.             }catch(Exception e){  
  47.                 e.printStackTrace();  
  48.             }  
  49.         }  
  50.     }  
  51.   
  52.     public DownUtil(String path, String targetFile, int threadNum) {  
  53.         this.path = path;  
  54.         this.threadNum = threadNum;  
  55.         this.threads = new DownThread[threadNum];  
  56.         this.targetFile = targetFile;  
  57.     }  
  58.   
  59.     public void download() throws Exception{  
  60.         URL url = new URL(path);  
  61.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  62.         conn.setConnectTimeout(5 * 1000);  
  63.         conn.setRequestMethod("GET");  
  64.         conn.setRequestProperty(  
  65.                 "Accept""image/gif, image/jpeg, image/pjpeg, image/pjpeg, "  
  66.                 + "application/x-shockwave-flash, application/xaml+xml, "  
  67.                 + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "  
  68.                 + "application/x-ms-application, application/vnd.ms-excel, "  
  69.                 + "application/vnd.ms-powerpoint, application/msword, */*");  
  70.         conn.setRequestProperty("Accept-Language""zh-CN");  
  71.         conn.setRequestProperty("Charset""UTF-8");  
  72.         conn.setRequestProperty("Connection""Keep-Alive");  
  73.   
  74.         fileSize = conn.getContentLength();  
  75.         conn.disconnect();  
  76.         int currentPartSize = fileSize / threadNum + 1;  
  77.         RandomAccessFile file = new RandomAccessFile(targetFile, "rw");  
  78.         file.setLength(fileSize);  
  79.         file.close();  
  80.         // Multi-threaded download  
  81.         for(int i = 0; i < threadNum; ++i){  
  82.             int startPos = i * currentPartSize;  
  83.             RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");  
  84.             currentPart.seek(startPos);  
  85.             threads[i] = new DownThread(startPos, currentPartSize, currentPart);  
  86.             threads[i].start();  
  87.         }  
  88.     }  
  89.     // Gets the percentage of completion of the download  
  90.     public double getCompleteRate(){  
  91.         int sumSize = 0;  
  92.         for(int i = 0; i < threadNum; ++i){  
  93.             sumSize += threads[i].length;  
  94.         }  
  95.         return sumSize * 1.0 / fileSize;  
  96.     }  
  97. }  

这是主程序:

[java] view plain copy
  1. public class MultiThreadedDownload {  
  2.   
  3.     public static void main(String[] args) throws Exception{  
  4.         DownUtil downUtil = new DownUtil("http://imgsrc.baidu.com/baike/pic" +  
  5.                 "/item/8644ebf81a4c510f64b65cfa6559252dd52aa5e4.jpg""liuyan.jpg"4);  
  6.         downUtil.download();  
  7.         new Thread(()->{  
  8.             while(downUtil.getCompleteRate() < 1){  
  9.                 System.out.println("已完成:" + downUtil.getCompleteRate());  
  10.                 try{  
  11.                     Thread.sleep(1000);  
  12.                 }catch(Exception e){  
  13.                     e.printStackTrace();  
  14.                 }  
  15.             }  
  16.         }).start();  
  17.     }  
  18. }  


谢谢浏览!
原创粉丝点击