-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiderTest.java
More file actions
112 lines (105 loc) · 2.48 KB
/
SpiderTest.java
File metadata and controls
112 lines (105 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package test.java.net;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.*;
import java.net.HttpURLConnection;
import java.util.Scanner;
public class SpiderTest {
//URL
private static String url="http://other.web.rd01.sycdn.kuwo.cn/resource/n1/18/27/633824864.mp3";
public static void main(String[] args) {
//下载方法
Scanner sc=new Scanner(System.in);
String m=sc.nextLine();
downLoadGoogleIcon(m);
}
/**
* 依据URL获取文件名,当文件名为空时 设置文件名为default.temp
* @param url
* @return
*/
private static String getFileNameFromUrl(String url) {
if(url==null) {
url="default.temp";
}else {
if(url.lastIndexOf("/")!=-1) {
url=url.substring(url.lastIndexOf("/")+1,url.length());
}else {
if(url.lastIndexOf(".")!=-1) {
url.substring(url.lastIndexOf(".")+1,url.length());
}else {
url="default.temp";
}
}
}
System.out.println("the file name is "+url);
return url;
}
/**
* 下载网络资源
* @param downFileURL
*/
private static void downLoadGoogleIcon(String downFileURL) {
HttpURLConnection httpConnection=null;//连接类
InputStream is=null;//输入流
FileOutputStream fos=null;//输出流 文件部分
String icon=downFileURL;
byte buf[]=null;
try {
if(icon==null) {
throw new Exception("访问的URL不存在,请重试.");
}
URL url=new URL(icon);
//文件路径
String path="/home/zd/downLoad";
httpConnection=(HttpURLConnection)url.openConnection();
is=httpConnection.getInputStream();
File pathFile=new File(path);
if(!pathFile.exists()) {
pathFile.mkdirs();
}
fos=new FileOutputStream(new File(path+"/"+getFileNameFromUrl(downFileURL)),true);
buf=new byte[1024*8];
int len=0;
int count=0;
while((len=is.read(buf))>0) {
count++;
fos.write(buf,0,len);
if(count==64) {
count=0;
try {
System.out.println("download is paused!!!");
Thread.sleep(1);
}catch(InterruptedException e) {
}
}
}
System.out.println("download is completed!!!");
httpConnection.disconnect();
is.close();
fos.flush();
fos.close();
buf=null;
httpConnection=null;
is=null;
fos=null;
}catch(Exception e) {
System.out.print("download error!!!");
}finally {
try {
if(is!=null) {
is.close();
}
is=null;
if(fos!=null) {
fos.close();
}
fos=null;
if(buf!=null)
buf=null;
}catch(Exception e) {
}
}
}
}