mydownload
package myDownload;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import javax.swing.JTextArea;
public class myDownload {
public static void main(String[] args) {
System.out.println("请输入:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
Downloader downloader = new Downloader(str);
// 启动下载线程
Thread thread = new Thread(downloader);
thread.start();
}
}
// 网络文件下载类
class Downloader implements Runnable {
// 网络文件的URL
String urlString;
// 构造函数
public Downloader(String urlString) {
// 设置属性
this.urlString = urlString;
}
// 下载网络文件的线程方法
public void run() {
// 网络文件的相关信息
StringBuffer info = new StringBuffer();
try {
// 网络文件的URL
URL url = new URL(urlString);
// 打开该网络文件的URL连接
URLConnection urlConn = url.openConnection();
// 添加网络文件的相关信息
info.append("主机: " + url.getHost() + "\n");
info.append("端口: " + url.getDefaultPort() + "\n");
info.append("网络文件的类型: " + urlConn.getContentType() + "\n");
info.append("长度: " + urlConn.getContentLength() + "\n");
info.append("正在下载...");
// 显示网络文件的相关信息
System.out.println(info);
// 创建网络文件的输入流
InputStream is = urlConn.getInputStream();
// 获取网络文件的文件名称
String localFileName = url.getFile().substring(
url.getFile().lastIndexOf("/") + 1);
System.out.println(localFileName);
// 创建本地文件输出流
FileOutputStream fos = new FileOutputStream(localFileName);
// 读取网络文件到本地文件
int data;
while ((data = is.read()) != -1) {
fos.write(data);
}
// 关闭流
is.close();
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("succeed");
}
}
package crixec.pub.download;
import java.io.*;
import java.net.*;
public class DownloadTask extends Thread
{
private DownloadConfig mDownloadConfig = null;
private int mTaskId;
private boolean FLAG = false;
private InputStream is = null;
private RandomAccessFile raf = null;
private URLConnection mURLConnection = null;
private URL mURL = null;
public DownloadTask(DownloadConfig mDownloadConfig, int mTaskId)
{
setTaskId(mTaskId);
setDownloadConfig(mDownloadConfig);
}
public void setTaskId(int mTaskId)
{
this.mTaskId = mTaskId;
}
public int getTaskId()
{
return mTaskId;
}
public void setDownloadConfig(DownloadConfig mDownloadConfig)
{
this.mDownloadConfig = mDownloadConfig;
}
public DownloadConfig getDownloadConfig()
{
return mDownloadConfig;
}
public void startDownloadTask()
{
if (!isRunning())
{
FLAG = true;
start();
}
}
public void stopDownloadTask()
{
if (!isRunning()) return;
FLAG = false;
interrupt();
System.out.println(">>>>>> " + FLAG);
DownloadManager.getInstance().mCallBack.onStop(this);
}
public boolean isRunning()
{
return FLAG;
}
@Override
public void run()
{
// TODO: Implement this method
super.run();
final File file = new File(mDownloadConfig.getSavePath(), mDownloadConfig.getSaveName());
long index = file.length();
long mFileLength = 0;
DownloadManager.getInstance().mCallBack.onStart(this);
while (FLAG)
{
try
{
raf = new RandomAccessFile(file, "rwd");
raf.seek(index);
mURL = new URL(mDownloadConfig.getDownloadUrl());
mURLConnection = mURL.openConnection();
mURLConnection.setConnectTimeout(15000);
mFileLength = mURLConnection.getContentLength();
is = mURLConnection.getInputStream();
is.skip(index);
int length = -1;
byte[] buffer = new byte[1024 * 1024];
long mStartTime = System.currentTimeMillis();
long mEndTime = 0;
long mReadedSum = 0;
while ((length = is.read(buffer)) != -1)
{
if (!FLAG) break;
mStartTime = System.currentTimeMillis();
mReadedSum += length;
raf.write(buffer, 0, length);
mEndTime = System.currentTimeMillis();
double speed = length / (mEndTime - mStartTime);
DownloadManager.getInstance().mCallBack.onProgress(this, mReadedSum, speed, mFileLength);
}
break;
}
catch (Exception e)
{
e.printStackTrace();
DownloadManager.getInstance().mCallBack.onFaliure(this);
}
finally
{
try
{
is.close();
raf.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
if (FLAG)
{
FLAG = false;
DownloadManager.getInstance().mCallBack.onFinish(this);
}
}
}