gpt4 book ai didi

java - 类似 IDM 的 java 应用程序

转载 作者:行者123 更新时间:2023-12-01 13:38:29 24 4
gpt4 key购买 nike

我正在用java编写一个像IDM这样的小型应用程序。但这有很多异常(exception)。这是Downloader类的代码,它实现了runnable,我想将它用于多线程。

public class Downloader implements Runnable{
private DataInputStream inputStream;
private byte[][] fileData;
private int index;
private int size;

public Downloader(DataInputStream inputStream, byte[][] fileData, int index, int size) {
this.inputStream = inputStream;
this.fileData = fileData;
this.index = index;
this.size = size;
}

public synchronized void run() {
try{
inputStream.skipBytes(index * size);
for(int i= 0;i<size;i++){
fileData[index][i] = inputStream.readByte();
System.out.println("It works : " + index);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}}

这是我的主课

public class Main {

public static void main(String[] args) {
String s;
//Scanner input = new Scanner(System.in);
//System.out.print("Enter file destination : ");
//s = input.nextLine();
s = "http://video.varzesh3.com/video/clip1/92/uclip/fun/gaf_6_borhani.mp4";
URL url;
URLConnection connection;
DataInputStream inputStream;
FileOutputStream outStream;
byte[][] fileData;
try{
url = new URL(s);
connection = url.openConnection();
inputStream = new DataInputStream(connection.getInputStream());
fileData = new byte[8][connection.getContentLength() / 4];
int size = connection.getContentLength() / 4;
Runnable d0 = new Downloader(inputStream, fileData, 0, size);
Runnable d1 = new Downloader(inputStream, fileData, 1, size);
Runnable d2 = new Downloader(inputStream, fileData, 2, size);
Runnable d3 = new Downloader(inputStream, fileData, 3, size);
Thread thread0 = new Thread(d0);
Thread thread1 = new Thread(d1);
Thread thread2 = new Thread(d2);
Thread thread3 = new Thread(d3);
thread0.start();
thread1.start();
thread2.start();
thread3.start();
inputStream.close();
String path = "C:\\Users\\NetTest\\Desktop\\test.mp4";
outStream = new FileOutputStream(new File(path));
outStream.write(fileData[0]);
/*outStream.write(fileData[1]);
outStream.write(fileData[2]);
outStream.write(fileData[3]);
outStream.write(fileData[4]);
outStream.write(fileData[5]);
outStream.write(fileData[6]);
outStream.write(fileData[7]);*/
outStream.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}}

但是当我运行它时,就会发生这种情况

It works: 0
null
null
null
null

我现在应该做什么?

最佳答案

您的代码中有 2 个问题。

  1. 在当前状态下,您close() InputStream,所有Thread在启动它们后尝试直接读取它们( -> 当他们运行时)。为了解决这个问题,你可以调用Thread类的join()方法。在您的情况下,您必须为所有 4 个线程调用它以确保它们完成。

  2. 如果我理解正确的话,您希望将下载的文件分成4个部分同时下载。

为此,您需要 4 个独立的 InputStreams。 (目前您正在使用 ONE [另请参阅:Java Object Copying ])

因此,要更改此设置,您的代码将如下所示:

public class Downloader implements Runnable{
private byte[][] fileData;
private int index;
private int size;
private URL url;

public Downloader(URL url, byte[][] fileData, int index, int size) {
this.fileData = fileData;
this.index = index;
this.size = size;
this.url = url;
}

public synchronized void run() {
try{
URLConnection connection = url.openConnection();
DataInputStream inputStream = new DataInputStream(connection.getInputStream());
inputStream.skipBytes(index * size);
for(int i= 0;i<size;i++){
fileData[index][i] = inputStream.readByte();
System.out.println("It works : " + index);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}

关于java - 类似 IDM 的 java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21053660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com