关于如何通过下载 URL 下载网络资源,最近做了一些整理

参考文章

差不多以下的几个都是可以 work

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
public class DownloadByUrl {

private static final String DOWNLOAD_URL = "https://archive.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz";
private static final String FILE_NAME = "download_file/maven-3.5.4-bin.tar.gz";


@Test
void downloadByJavaIO() throws Exception {
try (BufferedInputStream in = new BufferedInputStream(new URL(DOWNLOAD_URL).openStream())){
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME);
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer,0,1024)) != -1){
fileOutputStream.write(dataBuffer,0,bytesRead);
}
}

}

@Test
void downloadByJavaIOAndCopy() throws Exception{
InputStream in = new URL(DOWNLOAD_URL).openStream();
Files.copy(in, Paths.get(FILE_NAME), StandardCopyOption.REPLACE_EXISTING);
}

@Test
void downloadByNIO(){
try (FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME)){
ReadableByteChannel readableByteChannel = Channels.newChannel(new URL(DOWNLOAD_URL).openStream());
// get a file channel
;
// FileChannel fileChannel = fileOutputStream.getChannel();
fileOutputStream.getChannel()
.transferFrom(readableByteChannel,0,Long.MAX_VALUE);

} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
void downloadByCommonsIO() throws Exception{
FileUtils.copyURLToFile(
new URL(DOWNLOAD_URL)
,new File(FILE_NAME)
,1000
,2000
);

}
}
Edited on Views times