文件下载

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);
}
}


}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值