Morteza Asadi Morteza Asadi - 4 months ago 33x
Java Question

upload large file in spring using FileCopyUtils.copy

I'm using org.springframework.util.FileCopyUtils to upload files in my projects.

FileCopyUtils.copy(multipartFile.getBytes(), new FileOutputStream( basePath + "/" + uploadedfile.getFileName()));

It use a FileOutputStream for uploading file and for smaller file this work fine, But how I can upload file with 2GB size or higher?


According the @m-deinum's comment, I Finally use org.springframework.util.StreamUtils to upload large files:

StreamUtils.copy(multipartFile.getInputStream(), new FileOutputStream( basePath + "/" + uploadedfile.getFileName()));

I use an input stream instead of a byte[] for uploading files, also for read (downloading) files I use InputStream:

try {
        inputStream = new FileInputStream(basePath + "/" + fileName);
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        do {
            bytesRead =, 0, buffer.length);
            httpServletResponse.getOutputStream().write(buffer, 0, bytesRead);
        } while (bytesRead == buffer.length);

        /* some code for set attributes to httpServletResponse */
    } finally {
        if (inputStream != null)