标签:进度 ESS down 链接 div info 图片 etc tin
new Thread(new Runnable() { @Override public void run() { String path = mEt.getText().toString().trim(); if (TextUtils.isEmpty(path)) { Toast.makeText(MainActivity.this, "下载链接地址不能为空", Toast.LENGTH_SHORT).show(); } else { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); File file = new File(getFileName(path) + ".txt"); String path1 = file.getPath(); Log.d(TAG, "run: "+path1); boolean exists = file.exists(); Log.d(TAG, "run: "+exists); if (file.exists() && file.length() > 0) { FileInputStream fileInputStream = new FileInputStream(file); BufferedReader bfis = new BufferedReader(new InputStreamReader(fileInputStream)); String lastDownloadPosition = bfis.readLine(); mStartIndex = Integer.parseInt(lastDownloadPosition); conn.setRequestProperty("range","bytes="+mStartIndex+"-"); bfis.close(); } int code = conn.getResponseCode(); Log.d(TAG, "run: "+code); if (code ==200){ mLength = conn.getContentLength(); } if (code == 200||code ==206) { //设置进度条的最大进度 runOnUiThread(new Runnable() { @Override public void run() { mPb.setMax(mLength); } }); InputStream is = conn.getInputStream(); RandomAccessFile raf = new RandomAccessFile(getFileName(path),"rw"); long length = raf.length(); Log.d(TAG, "run: "+length); raf.seek(mStartIndex); int len = 0; mCurrentsize = mStartIndex; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { raf.write(buf, 0, len); if (isstop) { RandomAccessFile raff = new RandomAccessFile(getFileName(path) + ".txt", "rw"); raff.write(String.valueOf(mCurrentsize).getBytes()); raff.close(); break; } mCurrentsize += len; runOnUiThread(new Runnable() { @Override public void run() { mPb.setProgress(mCurrentsize); } }); } is.close(); raf.close(); if (mCurrentsize==mLength){ File deleteFile = new File(getFileName(path) + ".txt"); deleteFile.delete(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show(); } }); } } } catch (Exception e) { e.printStackTrace(); } } } }).start();
if (isstop) { RandomAccessFile raff = new RandomAccessFile(getFileName(path) + ".txt", "rw"); raff.write(String.valueOf(mCurrentsize).getBytes()); raff.close(); break; }
if (file.exists() && file.length() > 0) { FileInputStream fileInputStream = new FileInputStream(file); BufferedReader bfis = new BufferedReader(new InputStreamReader(fileInputStream)); String lastDownloadPosition = bfis.readLine(); mStartIndex = Integer.parseInt(lastDownloadPosition); conn.setRequestProperty("range","bytes="+mStartIndex+"-"); bfis.close(); }
if (mCurrentsize==mLength){ File deleteFile = new File(getFileName(path) + ".txt"); deleteFile.delete(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show(); } }); }
标签:进度 ESS down 链接 div info 图片 etc tin
原文地址:https://www.cnblogs.com/nangongyibin/p/10229009.html