明妍宁 2019-11-01 10:36 采纳率: 62.5%
浏览 329
已结题

如何使用可能是内部类,刚学java

package downutil;

import java.net.URL;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;

import java.lang.Thread;
import org.omg.CORBA_2_3.portable.InputStream;

public class DownUtil {
private String path;
private String targetFile;
private int threadNum;
private DownThread[] threads;
private int fileSize;

public DownUtil(String path, String targetFile, int threadNum) {
    this.path = path;
    this.threadNum = threadNum;

    //此处DownThread类不能用,以下如此,亦不能实例化
    threads = (DownThread[]) new DownThread(threadNum);


    this.targetFile = targetFile;
}

public void download() throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5 * 1000);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept",
            "image/gif,image/jpeg,image/pjpeg,image/pjpeg," + "application/x-shockwave-falsh,application/xaml+xml,"
                    + "application/vnd.ms-xpsdocument,application/x-ms-xbap,"
                    + "application/x-ms-application,application/vnd.ms-excel,"
                    + "application/vnd.ms-powerpoint,application/msword,*/*");
    conn.setRequestProperty("Accept-Language", "zh-CN");
    ;
    conn.setRequestProperty("Charset", "UTF-8");
    conn.setRequestProperty("Connection", "Keep-Alive");

    fileSize = conn.getContentLength();
    conn.disconnect();
    int currentPartSize = fileSize / threadNum + 1;
    RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
    file.setLength(fileSize);
    ;
    file.close();
    for (int i = 0; i < threadNum; i++) {
        int startPos = i * currentPartSize;
        RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
        currentPart.seek(startPos);
        ;

        //此处如此DownTread
        threads[i] = new DownThread(startPos, currentPartSize, currentPart);
        ((Thread) threads[i]).start();
    }
}

public double getCompleteRate() {
    int sumSize = 0;
    for (int i = 0; i < threadNum; i++) {

    //此处length报错,不知啥问题
        sumSize += threads[i].length;
    }

    return sumSize * 1.0 / fileSize;

//此处DownThread不知啥问题
private class DownThread extends Thread {
private int startPos;
private int currentPartSize;
private RandomAccessFile currentPart;

        public int length;

        public DownThread(int startPos, int currentPartSize, RandomAccessFile currentPart) {
            this.startPos = startPos;
            this.currentPartSize = currentPartSize;
            this.currentPart = currentPart;
        }

        public void run() {
            try {
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5 * 1000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Accept",
                        "image/gif,image/jpeg,image/pjpeg,image/pjpeg,"
                                + "applicaton/x-shockwave-flash,application/xaml+xml,"
                                + "spplication/vnd.ms-xpsdocument,application/x-ms-xbap,"
                                + "application/x-ms-application,application/vnd.ms-excel,"
                                + "appplication/vnd.ms-powerpoint,application/mswerd,*/*");
                conn.setRequestProperty("Accept-Language", "zh-CN");
                conn.setRequestProperty("Charset", "UTF-8");
                java.io.InputStream inStream = conn.getInputStream();
                inStream.skip(this.startPos);
                byte[] buffer = new byte[1024];
                int hasRead = 0;
                while (length < currentPartSize && (hasRead = inStream.read(buffer)) != -1)
                    ;
                {
                    currentPart.write(buffer, 0, hasRead);
                    length += hasRead;
                    currentPart.close();
                    inStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        };
    }
}

public static class MultiThreadDown {
    public static void main(String[] args) throws Exception {
        final DownUtil downUtil = new DownUtil(
                "http://www.crazyit.org/" + "attachments/month_1403/1403202355ff6cc9a4fbf6f14a.png", "ios.png", 4);
        downUtil.download();
        new Thread(() -> {
            while (downUtil.getCompleteRate() < 1) {
                System.out.println("已完成:" + downUtil.getCompleteRate());
                try {
                    Thread.sleep(1000);
                } catch (Exception ex) {
                }
            }
        }).start();
    }
}

}

  • 写回答

2条回答 默认 最新

  • dabocaiqq 2019-11-01 12:06
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr