80,337
社区成员
发帖
与我相关
我的任务
分享
package com.myy.dowloaderforandrop;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
protected static final int ERROR = 0;
public static int thread_count = 3;
public static int thread_num = thread_count;
EditText et;
ProgressBar pb;
Button but;
public Handler handler = new Handler()
{
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==ERROR)
{
Toast.makeText(MainActivity.this,(String)msg.obj,Toast.LENGTH_SHORT).show();
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et_url);
pb = (ProgressBar) findViewById(R.id.pb_down);
but = (Button) findViewById(R.id.but_down);
}
public void download(View view)
{
//连接服务器,获得文件长度并且在本地创建一个相同大小的文件
new Thread()
{
public void run()
{
try {
String path =et.getText().toString().trim();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(1000);
conn.setRequestMethod("GET");
int code =conn.getResponseCode();
if(code==200)
{
int len = conn.getContentLength();
int blocklen = len/thread_count;
System.out.println("文件总长度为"+len);
//创建一个长度相同的文件
RandomAccessFile raf = new RandomAccessFile("/sdcard/setup.exe","rwd");
raf.setLength(len);
raf.close();
for(int id = 1;id<=thread_count;id++)
{
int start = (id-1)*blocklen;
int end = id*blocklen-1;
if(id==thread_count)//为最后一个线程时,使他的结尾为总长度
{
end=len;
}
System.out.println("线程"+id+" start:"+start+" end: "+end);
new DownloadThread(path,id,start,end).start();
}
}
else
{
System.out.println("连接出错" );
Message msg = new Message();
msg.what=ERROR;
msg.obj="连接出错";
handler.sendMessage(msg);
}
} catch (Exception e) {
System.out.println("服务器出错" );
Message msg = new Message();
msg.what=ERROR;
msg.obj="服务器出错";
handler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
}
package com.myy.dowloaderforandrop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Message;
public class DownloadThread extends Thread {
public int id,start,end;
public String path;
/**
* @param path 下载路径
* @param id 线程id
* @param start 开始下载位置
* @param end 结束下载位置
*/
public DownloadThread(String path,int id, int start, int end) {
this.id = id;
this.start = start;
this.end = end;
this.path=path;
}
public void run() {
try {
File check = new File("/sdcard/"+id+".txt");
byte [] temp = new byte[1024];
int downloaded_len=0;
if(check.exists()&&check.length()>0)
{
FileInputStream fis = new FileInputStream(check);
int temp_len;
temp_len = fis.read(temp);
String down_str = new String(temp,0,temp_len);
downloaded_len = Integer.parseInt(down_str);
System.out.println("线程"+id+" 已下载: "+downloaded_len);
fis.close();
}
URL url;
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//重要:请求服务器只是下载部分内容且设置下载位
conn.setRequestProperty("Range","bytes="+(start+downloaded_len)+"-"+end);
System.out.println("线程"+id+" 下载位置: "+(start+downloaded_len));
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
//服务器请求全部资源成功返回200,请求部分资源成功返回203
int code =conn.getResponseCode();
System.out.println("线程"+id+" code: "+code);
InputStream is = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile("/sdcard/setup.exe","rwd");
// 记录已经下载的位置到文件夹
//定位
raf.seek(start+downloaded_len);
int len =0;
byte [] buffer = new byte[1024];
while((len=is.read(buffer))!=-1)
{
RandomAccessFile record = new RandomAccessFile("/sdcard/"+id+".txt","rwd");
//只有一个参数的write,结束位置为buffer的长度(结尾),若buffer未装满
//就会出错
raf.write(buffer,0,len);
downloaded_len+=len;
//将下载长度放入记录文件
record.write(String.valueOf(downloaded_len).getBytes());
record.close();
// System.out.println("线程"+id+" 下载位置: "+(start+downloaded_len));
}
is.close();
raf.close();
MainActivity.thread_num--;
System.out.println("线程"+id+"下载完毕");
} catch (Exception e) {
System.out.println("线程"+id+"出错");
// Message msg = new Message();
// msg.what=MainActivity.ERROR;
// msg.obj="线程"+id+"出错";
// MainActivity.handler.sendMessage(msg);
e.printStackTrace();
}finally
{
System.out.println("还有"+MainActivity.thread_num+"个线程");
if(MainActivity.thread_num==0)//所有线程已经完成
{
for(int id = 1;id<=MainActivity.thread_count;id++)
{
File dle_file = new File("/sdcard/"+id+".txt");
dle_file.delete();
System.out.println(id+".txt 已经删除");
}
}
}
}
}
package com.myy.dowloaderforandrop;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
protected static final int ERROR = 0;
public static int thread_count = 3;
public static int thread_num = thread_count;
EditText et;
ProgressBar pb;
Button but;
public Handler handler = new Handler()
{
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==ERROR)
{
Toast.makeText(MainActivity.this,(String)msg.obj,Toast.LENGTH_SHORT).show();
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et_url);
pb = (ProgressBar) findViewById(R.id.pb_down);
but = (Button) findViewById(R.id.but_down);
}
public void download(View view)
{
//连接服务器,获得文件长度并且在本地创建一个相同大小的文件
new Thread()
{
public void run()
{
try {
String path =et.getText().toString().trim();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(1000);
conn.setRequestMethod("GET");
conn.connect();
int code =conn.getResponseCode();
int len;
if(code==200)
{
len = conn.getContentLength();
int blocklen = len/thread_count;
System.out.println("文件总长度为"+len);
//创建一个长度相同的文件
RandomAccessFile raf = new RandomAccessFile("/sdcard/setup.exe","rwd");
raf.setLength(len);
raf.close();
for(int id = 1;id<=thread_count;id++)
{
int start = (id-1)*blocklen;
int end = id*blocklen-1;
if(id==thread_count)//为最后一个线程时,使他的结尾为总长度
{
end=len;
}
System.out.println("线程"+id+" start:"+start+" end: "+end);
new DownloadThread(path,id,start,end).start();
}
}
else
{
System.out.println("连接出错" );
Message msg = new Message();
msg.what=ERROR;
msg.obj="连接出错";
handler.sendMessage(msg);
}
} catch (Exception e) {
System.out.println("服务器出错" );
Message msg = new Message();
msg.what=ERROR;
msg.obj="服务器出错";
handler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
}
先贴着吧,以防有好心人帮我找......
谢谢楼上了 ,我再检查下吧