Java复习第一天---Javascript的基本知识点(二)

2014-11-23 17:52:35 · 作者: · 浏览: 43
小 accessFile.setLength(fileLength); // 关闭操作 accessFile.close(); // 首先计算出每个线程下载的大小 开始位置 结束位置 int threadSize = fileLength / threadNum; // for循环开启线程处理 for (int threadId = 1; threadId <= 3; threadId++) { int startIndex = (threadId - 1) * threadSize;// 开始位置 int endIndex = threadId * threadSize - 1; if (threadId == threadNum) {// 最后一个线程 endIndex = fileLength - 1; } System.out.println(当前线程-- + threadId + 开始位置--- + startIndex + 结束位置--- + endIndex + 线程大小---); // 开启线程下载要用Start方法 new DownLoadThread(threadId, startIndex, endIndex, spec, fileName).start(); } } else { MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, 设备的SD卡不可用, Toast.LENGTH_LONG) .show(); } }); } } else { // 这里用这个可以让它在主线程中运行 MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, 服务器端返回错误, Toast.LENGTH_LONG) .show(); } }); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } } class DownLoadThread extends Thread { private int threadId; private int startIndex; private int endIndex; private String path; private String fileName; public DownLoadThread(int threadId, int startIndex, int endIndex, String path, String fileName) { super(); this.threadId = threadId; this.startIndex = startIndex; this.endIndex = endIndex; this.path = path; this.fileName = fileName; } @Override public void run() { // 可以通过每个线程去下载文件了。 try { File sdfile = Environment.getExternalStorageDirectory(); // 获取每个线程下载的记录文件 File recordFile = new File(sdfile, threadId + .txt); // 判断记录文件是否存在 if (recordFile.exists()) { // 读取文件的内容 InputStream is = new FileInputStream(recordFile); // 利用工具类转换 String value = StreamTools.streamTostr(is); // 获取记录位置 int recordIndex = Integer.parseInt(value); // 记录的位置复制给开始的位置即可 startIndex = recordIndex; } // 通过path路径构建URL对象 URL url = new URL(path); // 通过URL对象的打开连接,返回对象 HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); // 设置请求头 httpURLConnection.setRequestMethod(GET); httpURLConnection.setConnectTimeout(5000); // 设置下载文件的开始位置和结束位置 httpURLConnection.setRequestProperty(Range, bytes= + startIndex + - + endIndex); // 获取的状态吗 int code = httpURLConnection.getResponseCode(); if (code == 206) { // 获取每个线程返回的流对象: InputStream inputStream = httpURLConnection .getInputStream(); // 获取文件名称 因为已经通过那边传过来了 // String fileName = // path.substring(path.lastIndexOf(/)+1); // 外部存储设备的路径 File file = new File(sdfile, fileName); // 根据文件创建她RandomAccessFile对象 RandomAccessFile raf = new RandomAccessFile(file, rwd); raf.seek(startIndex); // 定义读取的长度 int len = 0; byte buffer[] = new byte[50]; int total = 0; // 循环读取 while ((len = inputStream.read(buffer)) != -1) { System.out.println(当前线程-- + threadId + --已经下载了 + (startIndex + total)); // 创建线程下载记录的文件 RandomAccessFile threadfile = new RandomAccessFile( new File(sdfile, threadId + .txt), rwd); threadfile.writeBytes((startIndex + total) + ); threadfile.close(); raf.write(buffer, 0, len); total += len; synchronized (MainActivity.this) { currentProgress += len; progressBar.setProgress(currentProgress); // 计算百分比的操作 int final String percent = currentProgress * 100L / progressBar.getMax() + ; MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { tv_pb.setText(当前的进度是: + percent + %); } }); RandomAccessFile pbfile = new RandomAccessFile( new File(sdfile, pb.txt), rwd); pbfile.writeBytes(progressBar.getMax() + ; + currentProgress + ; + percent); pbfile.close(); } } raf.close(); inputStream.close(); MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { // System.out.println(当前线程----+threadId+下载完毕); Toast.makeText(MainActivity.this, 当前线程---- + threadId + 下载完毕, Toast.LENGTH_LONG).show(); } }); deleteRecordFiles(); } else { MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, 服务器端下载错误, Toast.LENGTH_LONG).show(); } }); } } catch (Exception e) { e.printStackTrace(); } } } public synchronized void deleteRecordFiles() { File sdfile = Environment.getExternalStorageDirectory(); System.out.println(-------------------------); threadRunning--; if (threadRunning == 0) { for (int i = 1; i <= 3; i++) { File recordFile = new File(sdfile, i + .txt); if (recordFile.exists()) { boolean flag1 = recordFile.delete();// 删除掉文件 System.out.println(下载记录 + i + 文件已 + flag1 + 删除); } File pdFile = new File(sdfile, pb.txt); if (pdFile.exists()) { boolean flag2 = pdFile.delete(); System.out.println(进度条记录文件已 + flag2 + 删除); } } } } public void displayMsg() { } }


BUG注意事项:

1、继续线程下载的文件一定要执行threadfile.close(),不然就无法执行后面的删除TXT文件部分。

2、一定要注意下载速度的设置,不能太快,超过传输文件的大小,不然会导致模拟器死掉。;