断点续传

mac2024-10-15  56

断点续传

断点续传httputilsThreadactivity

断点续传

package com.example.app2.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.URL; import java.net.URLConnection; public class DownLoadThread extends Thread { private static final String TAG = "ddd"; private String url_string; private String path; private Handler handler; public DownLoadThread(String url_string, String path, Handler handler) { this.url_string = url_string; this.path = path; this.handler = handler; } private long end; private long start; private long count = start; public static final int PROGRESS = 101; @Override public void run() { super.run(); File file = new File(path); InputStream is = null; RandomAccessFile raf = null; try { URL url = new URL(url_string); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); connection.connect(); if (connection.getResponseCode() == 200) { is = connection.getInputStream(); int len = 0; byte[] bytes = new byte[1024]; while ((len = is.read(bytes)) != -1) { end = connection.getContentLengthLong(); } Log.i(TAG, "end: "+end); } } catch (IOException e) { e.printStackTrace(); } if (file.exists()){ start = file.length(); }else { start = 0; } try { URL url = new URL(url_string); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); connection.setRequestProperty("Range","bytes="+start+"-"+end); connection.connect(); if (connection.getResponseCode() == 206) { InputStream is1 = connection.getInputStream(); int len = 0; byte[] bytes = new byte[1024]; raf = new RandomAccessFile(path, "rw"); Log.i(TAG, "start: "+start); raf.seek(start); count = start; while ((len = is1.read(bytes)) != -1) { raf.write(bytes,len,0); count += len; int progress = (int) (count*100/end); Message obtain = Message.obtain(); obtain.what = PROGRESS; obtain.obj = progress; Log.i(TAG, "progress: "+obtain.obj); handler.sendMessage(obtain); } } } catch (IOException e) { e.printStackTrace(); } } }

httputils

package com.example.day1031; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class Httputils { private static final String TAG = "Httputils"; public static String geturl(String s){ try { URL url = new URL(s); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); Log.i(TAG, "geturl: "+connection.getResponseCode()); if (connection.getResponseCode() == 200){ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); InputStream inputStream = connection.getInputStream(); int len = 0; byte[] bytes = new byte[1024]; while ((len = inputStream.read(bytes))!=-1){ byteArrayOutputStream.write(bytes,0,len); } return byteArrayOutputStream.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { } return null; } }

Thread

package com.example.day1031; 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.URL; import java.net.URLConnection; public class MyThread extends Thread { private static final String TAG = "MyThread"; private String url ; private String path; private Handler handler; public MyThread(String url, String path, Handler handler) { this.url = url; this.path = path; this.handler = handler; } private long start = 0; private long end; @Override public void run() { //第一次请求 try { URL url = new URL(this.url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); Log.i(TAG, "run: "+connection.getResponseCode()); if (connection.getResponseCode() == 200){ end = connection.getContentLength(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } File file = new File(path); if (file.exists()){ start = file.length(); }else { start = 0; } //第二次请求 try { URL url = new URL(this.url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); Log.i(TAG, "run: "+connection.getResponseCode()); if (connection.getResponseCode() == 200){ if (start == end){ handler.sendEmptyMessage(200); }else{ InputStream inputStream = connection.getInputStream(); RandomAccessFile rw = new RandomAccessFile(path, "rw"); rw.seek(start); long context = start; int len = 0; byte[] bytes = new byte[1024]; while ((len = inputStream.read(bytes))!=-1){ rw.write(bytes,0,len); context+=len; int progress = (int) (context*100/end); Message message = new Message(); message.what = 100; message.arg1 = progress; handler.sendMessage(message); } } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

activity

package com.example.day1031; import android.Manifest; import android.content.DialogInterface; import android.os.Build; import android.os.Handler; import android.os.Message; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SeekBar; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private ListView lv; private SeekBar bar; private List<JavaBean.DataBean> list = new ArrayList<>(); private String url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1"; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 100){ bar.setVisibility(View.VISIBLE); int arg1 = msg.arg1; bar.setProgress(arg1); if (arg1 == bar.getMax()){ bar.setVisibility(View.GONE); } } if (msg.what == 200){ Toast.makeText(MainActivity.this, "已经下载", Toast.LENGTH_SHORT).show(); } } }; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); bar = (SeekBar) findViewById(R.id.bar); //获取权限 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100); } //设置适配器 myAdapter = new MyAdapter(list,this); lv.setAdapter(myAdapter); //进行异步 Async(); //设置点击事件 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示"); builder.setMessage("是否下载这张图片"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { new MyThread(list.get(position).getPic(),"/storage/emulated/0/Movies/"+"pic"+position+".jpg",handler).start(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); } private void Async() { new MyAsync(list,myAdapter).execute(url); } }
最新回复(0)