package com.zeph.java261.singlethreaddl;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Stdownload {
public static void main(String[] args) {
URL url;
try {
url = new URL(
"http://www.stonept.org/attachments/201205/201205251005497fe0b82f54d2ad7fd3e8fafb84bd49d9.jpg");
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
String fileStr = url.getFile();
String fileName = fileStr.substring(fileStr.lastIndexOf('/') + 1);
FileOutputStream fos = new FileOutputStream("D://" + fileName);
byte[] buffer = new byte[1024];
int size = -1;
while ((size = is.read(buffer)) != -1) {
fos.write(buffer, 0, size);
}
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
单线程下载网络资源(摘自书:《你必须知道的261个Java问题》)
最新推荐文章于 2023-02-03 23:35:17 发布