Apache 公共文件上传“Streaming API" [英] Apache commons fileupload "Streaming API"

查看:26
本文介绍了Apache 公共文件上传“Streaming API"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我引用了 Apache Commons Page for Commons FileUpload

I quote from Apache Commons Page for Commons FileUpload

本页介绍了commons fileupload的传统API图书馆.传统的 API 是一种方便的方法.然而,对于终极性能,您可能更喜欢更快 Streaming API.

This page describes the traditional API of the commons fileupload library. The traditional API is a convenient approach. However, for ultimate performance, you might prefer the faster Streaming API.

我的问题

哪些具体差异使 Streaming API传统 API 更快?

What specific differences make Streaming API faster than traditional API?

推荐答案

关键区别在于您处理文件的方式,正如您自己在工厂类中注意到的那样.

The key difference is in the way you're handling the file, as you noticed by yourself with the factory class.

获取输入流时,流 API 未保存在磁盘中.最后,您将能够更快地处理文件(以临时内存为代价)...但我们的想法是避免将二进制文件保存在磁盘中,除非您真的想要/需要这样做.

The streaming API is not saving in disk while getting the input stream. In the end, you'll be able to handle the file faster (with a cost on temporary memory)... but the idea is to avoid saving the binary in disk unless you really want/need to.

之后,您当然可以使用缓冲输入流、字节数组或类似方法将数据保存到磁盘.

After that, you are able to save the data to disk, of course, using a bufferedinputstream, a byte array or similar.

打开流时的处理程序 (fileItemStreamElement.openStream() ) 是一个常见的 InputStream 实例.所以,你的如果它是一个大文件怎么办"的答案是这样的内存问题使用 Java 中的 InputStream

The handler when you open the stream ( fileItemStreamElement.openStream() ) is a common InputStream instance. So, the answer to your "what if it's a big file" is something like this Memory issues with InputStream in Java

流 API 不应保存到磁盘或保存在内存中.它只是提供了一个流,您可以从中读取以将文件复制到您想要的任何位置.这是一种避免使用临时目录并避免分配足够内存来保存文件的方法.这至少应该更快,因为它不会复制两次,一次从浏览器复制到磁盘/内存,然后再从磁盘/内存复制到保存它的位置.

The streaming API should not save to disk OR save in memory. It simply provides a stream you can read from to copy the file to where ever you want. This is a way to avoid having a temp directory and also avoid allocating enough memory to hold the file. This should be faster at least because it is not copied twice, once from the browser to disk/memory and then again from disk/memory to where ever you save it.

这篇关于Apache 公共文件上传“Streaming API"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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