Spring 分段文件上传究竟是如何工作的? [英] How does Spring multipart file upload exactly work?

查看:37
本文介绍了Spring 分段文件上传究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个文件服务器,它必须使用 Spring Boot 处理大文件上传 (>1GB).不想使用主存时如何实现上传?

I develope a fileserver that has to handle large file uploads (>1G B) with spring boot. How can I implement the upload when I do not want to use the main memory?

这是我的代码:

final String id = GenerationHelper.uuid();
    final File newFile = new File(id);
    LOG.info("New file: " + id + " with size " + content.getSize());
    if (!content.isEmpty()) {

        FileInputStream in = null;
        FileOutputStream out = null;
        long totalBytes = 0;

        try {
            in  = (FileInputStream) content.getInputStream();
            out = new FileOutputStream(newFile);

            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer);
                totalBytes += bytesRead;
                LOG.info(bytesRead);
            }
        } catch (IOException e) {
            LOG.error("Failed to save file", e);
            newFile.delete();
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                LOG.error("Error creating new file with id " + id + ". Deleting this file...", e);
            }
        }
        LOG.info(totalBytes + " Bytes read");
    }

文件完全上传后日志输出开始,所以我猜文件已经上传了.是否可以直接将上传写入文件系统?

The log output starts after the file has been uploaded completely so I guess that the file has been already uploaded. Is it possible to write an upload directly to the filesystem?

提前致谢!最大

推荐答案

分段上传将写入磁盘上的临时位置或保存在内存中.如 MultipartFile 的 javadoc 中所述,您有责任在请求处理结束时清理文件之前将文件移动到永久位置.

The multipart upload will be written to a temporary location on disk or held in memory. As explained in the javadoc for MultipartFile, it's then your responsibility to move the file to a permanent location before it's cleaned up at the end of the request processing.

文件内容要么存储在内存中,要么临时存储在磁盘上.在任何一种情况下,用户都有责任根据需要将文件内容复制到会话级或持久存储.临时存储将在请求处理结束时清除.

The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storages will be cleared at the end of request processing.

您可以通过调用 MultiPartFile.transferTo(File) 来移动内容(从内存或磁盘上的临时位置).

You can move the contents (from memory or the temporary location on disk) by calling MultiPartFile.transferTo(File).

文件是否保存在内存中或写入临时位置取决于底层实现.例如,Commons File Upload 会将小于 10240B 的文件存储在内存中,任何更大的文件都会写入磁盘.

Whether or not the file is held in memory or written to a temporary location depends on the underlying implementation. For example, Commons File Upload will store files less than 10240B in memory, with anything bigger being written to disk.

这篇关于Spring 分段文件上传究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆