断点续传
model包
package com.example.app3.model;
import android.os.Handler;
public interface HttpModel {
void get(String url);
void post(String url, String params, Handler handler);
void download(String url, String path, Handler handler);
void break_point(String url, String path, Handler handler);
}
package com.example.app3.model;
import android.os.Handler;
import com.example.app3.thread.BreakPointThread;
import com.example.app3.thread.DownloadThread;
import com.example.app3.thread.PostThread;
public class HttpModelImpl implements HttpModel {
@Override
public void get(String url) {
}
@Override
public void post(String url, String params, Handler handler) {
new PostThread(url,params,handler).start();
}
@Override
public void download(String url, String path, Handler handler) {
new DownloadThread(url,path,handler).start();
}
@Override
public void break_point(String url,String path,Handler handler) {
new BreakPointThread(url,path,handler).start();
}
}
thread包
package com.example.app3.thread;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
public class BreakPointThread extends Thread {
private String str;
private String path;
private Handler handler;
public BreakPointThread(String str, String path, Handler handler) {
this.str = str;
this.path = path;
this.handler = handler;
}
int max;
int start;
@Override
public void run() {
super.run();
try {
URL url = new URL(str);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
max=urlConnection.getContentLength();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File file = new File(path);
if(file.exists()){
start= (int) file.length();
}
Log.d("TAG", "start"+start+"end"+max);
try {
URL url = new URL(str);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Range","bytes="+start+"-"+max);
urlConnection.connect();
if(urlConnection.getResponseCode()==206){
Log.d("TAG", "run: 第二次");
InputStream inputStream = urlConnection.getInputStream();
RandomAccessFile randomAccessFile= new RandomAccessFile(path,"rw");
randomAccessFile.seek(start);
byte[] bytes=new byte[1024];
int len=0;
int count=start;
while((len=inputStream.read(bytes))!=-1){
randomAccessFile.write(bytes,0,len);
count+=len;
Message obtain = Message.obtain();
obtain.what=103;
obtain.obj=count*100/max;
handler.sendMessage(obtain);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.example.app3.thread;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.security.auth.login.LoginException;
public class DownloadThread extends Thread {
private String str;
private String path;
private Handler handler;
public DownloadThread(String str, String path, Handler handler) {
this.str = str;
this.path = path;
this.handler = handler;
}
@Override
public void run() {
super.run();
try {
URL url = new URL(str);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int max = urlConnection.getContentLength();
Log.i("TAG", "run: "+max);
if(urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(path);
byte[] bytes=new byte[1024];
int len=0;
int count=0;
while((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
count+=len;
Message obtain = Message.obtain();
obtain.what=103;
obtain.obj=count*100/max;
handler.sendMessage(obtain);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.example.app3.thread;
import android.os.Handler;
import android.os.Message;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class PostThread extends Thread {
private String str;
private String parms;
Handler handler;
public PostThread(String str, String parms, Handler handler) {
this.str = str;
this.parms = parms;
this.handler = handler;
}
@Override
public void run() {
super.run();
StringBuffer stringBuffer=new StringBuffer();
try {
URL url = new URL(str);
HttpURLConnection urlConnection = null;
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(60*1000);
urlConnection.setReadTimeout(60*1000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.getOutputStream().write(parms.getBytes());
urlConnection.connect();
byte[] bytes=new byte[1024];
int len=0;
InputStream inputStream = urlConnection.getInputStream();
if(urlConnection.getResponseCode()==200){
while ((len=inputStream.read(bytes))!=-1){
stringBuffer.append(new String(bytes,0,len));
}
Message obtain = Message.obtain();
obtain.what=101;
obtain.obj=stringBuffer.toString();
handler.sendMessage(obtain);
}else{
Message obtain = Message.obtain();
obtain.what=102;
obtain.obj="失败";
handler.sendMessage(obtain);
}
} catch (IOException e) {
Message obtain = Message.obtain();
obtain.what=102;
obtain.obj=e.getMessage();
handler.sendMessage(obtain);
}
}
}
package com.example.app3;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.app3.model.HttpModel;
import com.example.app3.model.HttpModelImpl;
public class MainActivity extends AppCompatActivity {
private Button upload;
private Button load;
private Button breakpoint;
private ProgressBar pb;
private HttpModel model;
Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 101){
Toast.makeText(MainActivity.this, ""+msg.obj.toString(), Toast.LENGTH_SHORT).show();
}else if (msg.what == 102){
Toast.makeText(MainActivity.this, "失败了", Toast.LENGTH_SHORT).show();
}else if (msg.what == 103){
pb.setProgress((Integer) msg.obj);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
model = new HttpModelImpl();
upload = (Button) findViewById(R.id.upload);
load = (Button) findViewById(R.id.load);
breakpoint = (Button) findViewById(R.id.breakpoint);
pb = (ProgressBar) findViewById(R.id.pb);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.post("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&","itemid=2&act=ad_app",handler);
}
});
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.download("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4","/sdcard/Movies/shkhs.mp4",handler);
}
});
breakpoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.download("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4","/sdcard/Movies/shkhs.mp4",handler);
}
});
}
}