80,361
社区成员
发帖
与我相关
我的任务
分享
package com.getsre;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "com.getsre";
private String Urlpath;
String file = "data/data/com.getsre/";
private File f;
List list;
private int threadNum = 3;
private LinearLayout tv;
private EditText edname;
private EditText edurl;
private Button btgo;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
list = new ArrayList();
switch (msg.what) {
case 0:
list.add("线程0开始下载……");
break;
case 1:
list.add("线程1开始下载……");
break;
case 2:
list.add("线程2开始下载……" + "\n" + "文件大小:" + msg.obj);
break;
case 20:
list.add("线程0完成");
break;
case 21:
list.add("线程1完成");
break;
case 22:
list.add("线程2完成");
break;
default:
break;
}
for (Object Info : list) {
TextView ChildView;
ChildView = new TextView(MainActivity.this);
ChildView.setTextSize(23);
ChildView.setTextColor(Color.BLACK);
ChildView.setText(String.valueOf(Info));
tv.addView(ChildView);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (LinearLayout) findViewById(R.id.tv);
edname = (EditText) findViewById(R.id.ed_name);
edurl = (EditText) findViewById(R.id.ed_url);
btgo = (Button) findViewById(R.id.bt);
Urlpath=edurl.getText().toString();
// Urlpath = String.valueOf(edurl.getText());
f = new File(file + String.valueOf(edname.getText()));
btgo.setOnClickListener(this);
}
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
test();
}
}).start();
}
public void test() {
try {
URL url = new URL(Urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
long size = conn.getContentLength();
long block = size / threadNum;
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.setLength(size);
raf.close();
Log.i(TAG, "文件大小= " + size);
for (int i = 0; i < threadNum; i++) {
long startpos = i * block;
long endpos = block * (i + 1) - 1;
if (i == threadNum) {
endpos = size;
}
Log.i(TAG, "线程" + i + "下载的部分为:" + startpos + "---" + endpos);
Message msg = new Message();
msg.what = i;
String b = Convert(size);
msg.obj = b;
handler.sendMessage(msg);
new Download(i, startpos, endpos).start();
}
} else {
Log.i(TAG, "访问失败: responseCode = " + code);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String Convert(long size) {
if (size > 1024) {
long d = size / (1024 * 1024);
return d + "MB";
} else return size + "字节";
}
class Download extends Thread {
private int i = 0;
private long startpos;
private long endpos;
public Download(int i, long startpos, long endpos) {
this.i = i;
this.startpos = startpos;
this.endpos = endpos;
}
@Override
public void run() {
try {
URL url = new URL(Urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("range","bytes="+startpos+"-"+endpos);
if (conn.getResponseCode()==206) {
InputStream is = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.seek(startpos);
Read(is, raf);
Log.i(TAG,"线程" + i + "下载完毕...");
Message msg=new Message();
msg.what=i+20;
// msg.obj="下载完成";
handler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
public void Read(InputStream is, RandomAccessFile raf) {
int len = 0;
byte[] buffer = new byte[1024];
try {
while ((len = is.read(buffer)) != -1) {
raf.write(buffer, 0, len);
}
is.close();
} catch (Exception e) {
Log.i(TAG, "运行Read方法出现异常!!!!!!!!!!!!!!!");
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.getsre.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="370dp">
<LinearLayout
android:id="@+id/tv"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL地址:"/>
<EditText
android:id="@+id/ed_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="输入URL地址"/>
<!--android:text="http://dlsw.baidu.com/sw-search-sp/soft/24/13406/XiuXiu_V4.0.1.2002_BdSetup.1437647987.exe"-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重命名:"/>
<EditText
android:id="@+id/ed_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="重命名加上后缀"/>
<!--android:text="XiuXiu_V4.0.1.2002_BdSetup.1437647987.exe"-->
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="GO" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>