DataOutputStream写得太多了

Luk*_*uth -1 java

我现在有什么

我目前正在尝试用Java创建一个小的下载管理器,我在将一个加载的字节写入文件时遇到了问题.我正在使用a DataOutputStream来编写我从a读取的字节数组DataInputStream.这是我创建的类:

public class DownloadThread extends Thread{

    private String url_s;
    private File datei;

    public DownloadThread(String url_s, File datei){
        this.url_s = url_s;
        this.datei = datei;
    }

    public void run(){
        // Connect:
        int size = 0;
        URLConnection con = null;
        try {
            URL url = new URL(url_s);
            con = url.openConnection();
            size = con.getContentLength();
            // Set Min and Max of the JProgressBar
            prog.setMinimum(0);
            prog.setMaximum(size);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Download:
        if (con != null || size != 0){
            byte[] buffer = new byte[4096];
            DataInputStream down_reader = null;
            // Output:
            DataOutputStream out = null;
            try {
                out = new DataOutputStream(new FileOutputStream(datei));
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            // Load:
            try {
                down_reader = new DataInputStream(con.getInputStream());
                int byte_counter = 0;
                int tmp = 0;
                int progress = 0;
                // Read:
                while (true){
                    tmp = down_reader.read(buffer);
                    // Check for EOF
                    if (tmp == -1){
                        break;
                    }
                    out.write(buffer);
                    out.flush();
                    // Set Progress:
                    byte_counter += tmp;
                    progress = (byte_counter * 100) / size;
                    prog.setValue( byte_counter );
                    prog.setString(progress+"% - "+byte_counter+"/"+size+" Bytes");
                }
                // Check Filesize:
                prog.setString("Checking Integrity...");
                if (size == out.size()){
                    prog.setString("Integrity Check passed!");
                } else {
                    prog.setString("Integrity Check failed!");
                    System.out.println("Size: "+size+"\n"+
                            "Read: "+byte_counter+"\n"+
                            "Written: "+out.size() );
                }
                // ENDE
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                try {
                    out.close();
                    down_reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        // Clean Up...
        load.setEnabled(true);
        try {
            this.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是一个内部类,prog-Object是一个JProgressBar来自它的母类,所以它可以直接访问.

我正在尝试下载TinyCC的Windows .exe版本,其大小应为281KB.我下载经理下载的文件大小为376KB.

脚本的输出如下所示:

Size: 287181
Read: 287181
Written: 385024
Run Code Online (Sandbox Code Playgroud)

因此,看起来读取的字节与文件大小匹配,但写入的字节数更多.我在这里错过了什么?

Kaj*_*Kaj 8

这是错的:

out.write(buffer);
Run Code Online (Sandbox Code Playgroud)

它应该是

out.write(buffer, 0, tmp);
Run Code Online (Sandbox Code Playgroud)

您需要指定要写入的字节数,读取并不总是读取完整的缓冲区.