java 文件下载 迅雷下载_java下载文件

1 packagecom.ljd.wallpaper.utils;2

3 importjava.io.BufferedReader;4 importjava.io.File;5 importjava.io.FileInputStream;6 importjava.io.FileNotFoundException;7 importjava.io.IOException;8 importjava.io.InputStream;9 importjava.io.InputStreamReader;10 importjava.io.RandomAccessFile;11 importjava.net.HttpURLConnection;12 importjava.net.URL;13 importjava.util.ArrayList;14 importjava.util.List;15

16 public classDownload {17

18 //迅雷下载 弱爆了 看看这个

19 public static voidmain(String[] args) {20

21 Download download = newDownload();22 download.run();23

24 }25

26 public voidrun() {27 List list = new ArrayList();28 try{29 FileInputStream fis = new FileInputStream(new File("/alidata1/linshi/zfy/picurl.txt"));30 InputStreamReader isr = newInputStreamReader(fis);31 BufferedReader br = newBufferedReader(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 voiddownloadpic(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 = newURL(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

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 longgetFileSize(String s) {152

153 int nFileLength = -1;154

155 try{156

157 URL url = newURL(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 returnnFileLength;197

198 }199 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值