单线程下载.java
/**
* FileOutputStream 类创建输出流对象, 然后使用 write() 方法, 将从输入流获得的网络资源保存到磁盘上, 实现网络资源的单线程下载.
**/
public void download(String urlAddr) {
try {
URL url = new URL(urlAddr);
URLConnection urlConn = url.openConnection();
urlConn.connect();
InputStream in = urlConn.getInputStream();
String filePath = url.getFile();
int pos = filePath.lastIndexOf("/")
String fileName = filePath.substring(pos + 1);
FileOutputStream out = new FileOutputStream("D:/" + fileName);
byte[] bt = new byte[1024];
int len = in.read(bt);
while(len != -1) {
out.write(bt, 0, len);
len = in.read(bt);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
断点续传.java
public void download(long startPosition, long endPosition) {
try {
URL url = new URL(urlAddress);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("User-Agent", "NetFox");
String rangeProperty = "bytes=" + startPosition + "-";
if (endPosition > 0) {
rangeProperty += endPosition;
}
conn.setRequestProperty("RANGE", rangeProperty);
conn.connect();
InputStream in = conn.getInputStream();
String file = url.getFile();
String name = file.substring(file.lastIndexOf("/") + 1);
FileOutputStream out = new FileOutputStream("D:/" + name, true);
byte[] buff = new byte[2048];
int len = 0;
len = in.read(buff);
while (len != -1) {
out.write(buff, 0, len);
len = in.read(buff);
}
out.close();
in.close();
conn.disconnect();
if ( readToPos > 0 && readToPos == totalLength ) {
System.exit(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
多线程下载.java
public class DownloadMultiThread implements Runnable {
private String sURL = null;
private File desFile;
private long startPos;
private long endPos;
public DownloadMultiThread() {
}
public DownloadMultiThread(String sURL, File desFile, long startPos, long endPos) {
this.sURL = sURL;
this.desFile = desFile;
this.startPos = startPos;
this.endPos = endPos;
}
public void run() {
RandomAccessFile out = new RandomAccessFile(desFile, "rw");
out.seek(startPos);
InputStream in = conn.getInputStream();
BufferedInputStream bin = new BufferedInputStream(in);
byte[] buff = new byte[2048];
int len = -1;
len = bin.read(buff);
while (len != -1) {
out.write(buff, 0, len);
len = bin.read(buff);
}
}
}
public void download(String url, String dest, int threadNum) throws Exception {
URL downURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection)downURL.openConnection();
long fileLength = -1;
int stateFlagCode = conn.getResponseCode();
if (stateFlagCode == 200) {
fileLength = conn.getContentLength();
conn.disconnect();
}
if (fileLength > 0) {
long byteCounts = fileLength / threadNum + 1;
File file = new File(dest);
int i = 0;
while (i < threadNum) {
long startPosition = byteCounts * i;
long endPosition = byteCounts * (i + 1);
if (i == threadNum -1) {
DownloadMultiThread fileThread = new DownloadMultiThread(url, file, startPosition, 0);
new Thread(fileThread).start();
} else {
DownloadMultiThread fileThread = new DownloadMultiThread(url, file, startPosition, endPosition);
new Thread(fileThread).start();
}
i++;
}
JOptionPane.showMessageDialog(null, "完成网络资源下载");
}
}
获取网页内容.java
public Collection<String> getURLCollection(String urlString) {
URL url = null;
URLConnection conn = null;
Collection<String> urlCollection = new ArrayList<String>();
try {
url = new URL();
conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String nextLine = br.readLine();
while (nextLine != null) {
urlCollection.add(nextLine);
nextLine = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
return urlCollection;
}