Kennan Wang 2017-11-02 07:56 采纳率: 6.7%
浏览 824

Java多线程下载的问题

package com.briup.day24;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadThread extends Thread {

private static final Object o=new Object();

private int id;
private String path;
private int startIndex;
private int endIndex;

public DownloadThread(int id, String path, int startIndex, int endIndex) {
    // TODO Auto-generated constructor stub
    this.id = id;
    this.path = path;
    this.startIndex = startIndex;
    this.endIndex = endIndex;
}

@Override
public void run() {
    // TODO Auto-generated method stub

    URL url;
    try {

        File file = new File(id + ".txt");
        int lastProgress = 0;

        if (file.exists()) {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            lastProgress = Integer.parseInt(br.readLine());
            startIndex+=lastProgress;
            br.close();
        }

        url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setReadTimeout(8000);
        connection.setConnectTimeout(8000);
        connection.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
        connection.connect();

        if (connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL) {

            InputStream inputStream = connection.getInputStream();

            byte[] arr = new byte[1024];

            int len = -1;
            int total = lastProgress;

            RandomAccessFile raf = new RandomAccessFile("download.jpg", "rwd");

            raf.seek(startIndex);

            RandomAccessFile raff = null;
            while ((len = inputStream.read(arr)) != -1) {
                // 写入到本地文件中
                sleep(100);
                raf.write(arr, 0, len);
                total += len;
                System.out.println("第" + id + "条线程下载了" + total);
                raff = new RandomAccessFile(file, "rwd");
                raff.write((total + "").getBytes());
            }


            raff.close();

            raf.close();

            synchronized (o) {
                Main.finishedThreadCount++;

                 if(Main.finishedThreadCount==Main.THREADCOUNT) {
                     //删除
                         for(int i=0;i<Main.THREADCOUNT;i++) {
                                 File f=new File(i+".txt");
                                 f.delete();
                         }
                 }
            }

        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

package com.briup.day24;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

/**

  • 多线程断点下载 1、先去磁盘上创建一个要下载的文件大小一样的空文件 2、计算每个线程的下载区间
  • @author yuxiangrousigaifan
    *
    */
    public class Main {

    public static final String PATH = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508931223542&di=69e33a551ab94504d1124b9ba28ab3c2&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0bd162d9f2d3572c329d50f18113632762d0c3a4.jpg";
    public static final int THREADCOUNT = 3;

    public static int finishedThreadCount = 0;

    public static void main(String[] args) throws IOException {

    URL url = new URL(PATH);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setReadTimeout(8000);
    connection.setConnectTimeout(8000);
    connection.connect();
    
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
    
        int size = connection.getContentLength();
    
        System.out.println(size);
        File file = new File("download.jpg");
    
        RandomAccessFile raf = new RandomAccessFile(file, "rwd");
        raf.setLength(size);
    
        raf.close();
    
        //
    
        int length = size / THREADCOUNT;
        for (int id = 0; id < THREADCOUNT; id++) {
            int startIndex = id * length;
            int endIndex = (id + 1) * length - 1;
    
            if (id == THREADCOUNT - 1) {
                endIndex = size - 1;
            }
    
            System.out.println("编号为" + id + "的线程的现在区间是" + startIndex + "----" + endIndex);
    
            new DownloadThread(id, PATH, startIndex, endIndex).start();
        }
    
    }
    

    }

}

用这个程序下载之后 在工程下面一直保留三个临时下载记录 删除不掉 ,但是我在线程类最后用了delete()方法呀 求大佬们解答

  • 写回答

1条回答 默认 最新

  • 呓语的风 2017-11-04 01:39
    关注

    你把删除放在线程执行完毕后试一下,就是你第二个方法后面

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看