上传大文件时OOM

OOM while uploading large file

我需要将一个非常大的文件从我的机器上传到服务器。 (几GB) 目前,我尝试了以下方法,但我一直在努力。

 Caused by: java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3236)

我可以增加内存,但这不是我想做的事情,因为我不确定我的代码会在哪里 运行。我想读几个 MB/kb 发送到服务器并释放内存并重复。尝试了其他方法,如 Files utils 或 IOUtils.copyLarge 但我遇到了同样的问题。

URL serverUrl =
                new URL(url);
    HttpURLConnection urlConnection = (HttpURLConnection) serverUrl.openConnection();

    urlConnection.setConnectTimeout(Configs.TIMEOUT);
    urlConnection.setReadTimeout(Configs.TIMEOUT);

    File fileToUpload = new File(file);

    urlConnection.setDoOutput(true);
    urlConnection.setRequestMethod("POST");
    urlConnection.addRequestProperty("Content-Type", "application/octet-stream");

    urlConnection.connect();

    OutputStream output = urlConnection.getOutputStream();
    FileInputStream input = new FileInputStream(fileToUpload);
    upload(input, output);
            //..close streams



private static long upload(InputStream input, OutputStream output) throws IOException {
        try (
                ReadableByteChannel inputChannel = Channels.newChannel(input);
                WritableByteChannel outputChannel = Channels.newChannel(output)
        ) {
            ByteBuffer buffer = ByteBuffer.allocateDirect(10240);
            long size = 0;

            while (inputChannel.read(buffer) != -1) {
                buffer.flip();
                size += outputChannel.write(buffer);
                buffer.clear();
            }

            return size;
        }
    }

我认为这与this有关,但我无法弄清楚我做错了什么。

另一种方法是,但我遇到了同样的问题:

private static long copy(InputStream source, OutputStream sink)
            throws IOException {
        long nread = 0L;
        byte[] buf = new byte[10240];
        int n;
        int i = 0;
        while ((n = source.read(buf)) > 0) {
            sink.write(buf, 0, n);
            nread += n;
            i++;
            if (i % 10 == 0) {
                log.info("flush");
                sink.flush();
            }
        }
        return nread;
    }

对 Denis Tulskiy 链接到的重复问题使用 setFixedLengthStreamingMode as per this answer

conn.setFixedLengthStreamingMode((int) fileToUpload.length());

来自文档:

This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.

目前,您的代码正在尝试将文件缓冲到 Java 的堆内存中,以便计算 HTTP 请求的 Content-Length header。