2 years ago

#19128

test-img

Givou

Java doesn't download full file size

I have a problem with downloading a file using Java.

This is my current code:

    public void download() {
        try {
            URL url = new URL("http://updates.thiemoo.at/index.php?requestupdate=cFceR9crB4SNVGv4bejKUa3DDMAJkYJa");
            URLConnection conn = url.openConnection();
            InputStream is = new BufferedInputStream(conn.getInputStream());
            OutputStream os = new BufferedOutputStream(new FileOutputStream(Main.main.getDataFolder().getAbsolutePath().toString()+"/Timoo.jar"));
            byte[] chunk = new byte[1024];
            int chunkSize;
            while ((chunkSize = is.read(chunk)) != -1) {
                os.write(chunk, 0, chunkSize);
            }
            os.flush();
            os.close();
            is.close();


        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

Most of the time I need to download other jar files from there which are around 20mb big. The downloaded file in this case has 1kb and is corrupted.

I don't get any errors.

UPDATE:
I tried to download the txt file which is provided in the code above. The file has following content:

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>

I hand over my update files with php like this, this seems to be a problem for java I guess. With my browser it works like a charm. Does anybody have a idea about this?

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

java

download

io

fileoutputstream

bufferedinputstream

0 Answers

Your Answer

Accepted video resources