首页 > 技术文章 > java下载文件

zfy0098 2016-05-17 11:37 原文

  1 package com.ljd.wallpaper.utils;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileNotFoundException;
  7 import java.io.IOException;
  8 import java.io.InputStream;
  9 import java.io.InputStreamReader;
 10 import java.io.RandomAccessFile;
 11 import java.net.HttpURLConnection;
 12 import java.net.URL;
 13 import java.util.ArrayList;
 14 import java.util.List;
 15 
 16 public class Download {
 17 
 18     // 迅雷下载 弱爆了 看看这个
 19     public static void main(String[] args) {
 20 
 21         Download download = new Download();
 22         download.run();
 23 
 24     }
 25 
 26     public void run() {
 27         List<String> list = new ArrayList<String>();
 28         try {
 29             FileInputStream fis = new FileInputStream(new File("/alidata1/linshi/zfy/picurl.txt"));
 30             InputStreamReader isr = new InputStreamReader(fis);
 31             BufferedReader br = new BufferedReader(isr);
 32 
 33             String x = "";
 34             while ((x = br.readLine()) != null) {
 35                 list.add(x);
 36             }
 37             br.close();
 38             isr.close();
 39             fis.close();
 40         } catch (FileNotFoundException e) {
 41             e.printStackTrace();
 42         } catch (IOException e) {
 43             e.printStackTrace();
 44         }
 45 
 46         for (int i = 0; i < list.size(); i++) {
 47             downloadpic(list.get(i));
 48         }
 49     }
 50 
 51     public void downloadpic(String imgurl) {
 52 
 53         // 下载链接
 54         // imgurl =
 55         // "http://vod81.c20.lixian.vip.xunlei.com/download?fid=Fa6JYKhSktIFAC9CS/"
 56         // +
 57         // "vSiPqvKcgRZYoBAAAAAE+OzDDQMKcK1JvL8LnS4EJ0IzVi&mid=666&threshold=150&tid="
 58         // + "69F8077525A6DBF6225FDA76633D456C&srcid=6&verno=1&g="
 59         // +
 60         // "4F8ECC30D030A70AD49BCBF0B9D2E04274233562&ui=xlkuaichuan&s=25847057&"
 61         // + "pk=kuaichuan&ak=8:0:999:0&e=1427397980&ms=1433600&ci=&ck="
 62         // +
 63         // "27868D3F2491379692DDBA74FA8A3353&at=3129FA76E995680A00968DB0BD6756D1&"
 64         // + "n=03A556D175706A6A678744C13A71617200&k=1&ts=1425521078";
 65         // 开始从哪个下载(默认为0)
 66         int nStartPos = 0;
 67         // 每次读取下载的字节数目(默认2048)
 68 
 69         int nRead = 2048;
 70 
 71         // 下载成功的文件名称
 72 
 73         // String sName = "tortoisehg-3.2.2-x64.rar";
 74         String sName = imgurl.substring(imgurl.lastIndexOf("/") + 1);
 75 
 76         // 保存的文件目录
 77 
 78         System.out.println(sName);
 79 
 80         String sPath = "/alidata1/linshi/zfy/pic6p/";
 81 
 82         if (new File(sPath + sName).exists()) {
 83             System.out.println("文件存在");
 84             return;
 85         }
 86 
 87         try {
 88 
 89             URL url = new URL(imgurl);
 90 
 91             // 打开连接
 92 
 93             HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
 94 
 95             // 获得文件长度
 96 
 97             long nEndPos = getFileSize(imgurl);
 98 
 99             RandomAccessFile oSavedFile = new RandomAccessFile(sPath + sName,"rw");
100 
101             httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
102 
103             String sProperty = "bytes=" + nStartPos + "-";
104 
105             httpConnection.setRequestProperty("RANGE", sProperty);
106 
107             InputStream input = httpConnection.getInputStream();
108 
109             byte[] b = new byte[nRead];
110 
111             int sum = 0;
112 
113             int nwrite = 0;
114 
115             // 读取网络文件,写入指定的文件中
116 
117             while ((nwrite = input.read(b, 0, nRead)) > 0 && nStartPos < nEndPos) {
118 
119                 oSavedFile.write(b, 0, nwrite);
120 
121                 nStartPos += nwrite;
122 
123                 ++sum;
124 
125                 // 每下载200kb打印一次
126 
127                 if (sum % (1024 * 256 / nRead) == 0)
128 
129                     System.out.println("已下载:"
130                             + (String.format("%.4f",(double) nStartPos / 1024 / 1024))
131                             + "MB        "
132                             + String.format("%.2f", (double) nStartPos * 100 / nEndPos) + "%");
133 
134             }
135 
136             oSavedFile.close();
137 
138             httpConnection.disconnect();
139 
140             System.out.println("---下载完成---");
141 
142         } catch (Exception e) {
143 
144             e.printStackTrace();
145 
146         }
147     }
148 
149     // 获得文件长度
150 
151     public static long getFileSize(String s) {
152 
153         int nFileLength = -1;
154 
155         try {
156 
157             URL url = new URL(s);
158 
159             HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
160 
161             httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
162 
163             int responseCode = httpConnection.getResponseCode();
164 
165             if (responseCode >= 400) {
166                 System.err.println("Error Code : " + responseCode);
167                 return -2;
168             }
169 
170             String sHeader;
171 
172             for (int i = 1;; i++) {
173 
174                 sHeader = httpConnection.getHeaderFieldKey(i);
175 
176                 if (sHeader != null) {
177                     if (sHeader.equals("Content-Length")) {
178                         nFileLength = Integer.parseInt(httpConnection.getHeaderField(sHeader));
179                         break;
180                     }
181                 } else{
182                     break;
183                 }
184             }
185 
186         } catch (IOException e) {
187             e.printStackTrace();
188         } catch (Exception e) {
189             e.printStackTrace();
190         }
191 
192         System.out.println("文件大小为:"
193                 + String.format("%.4f", (double) nFileLength / 1024 / 1024)
194                 + "MB\n");
195 
196         return nFileLength;
197 
198     }
199 }

 

推荐阅读