简体   繁体   English

Apache commons fileupload“Streaming API”

[英]Apache commons fileupload “Streaming API”

I quote from Apache Commons Page for Commons FileUpload 我引用了Commons FileUpload Apache Commons页面

This page describes the traditional API of the commons fileupload library. 该页面描述了commons文件上载库的传统API。 The traditional API is a convenient approach. 传统的API是一种方便的方法。 However, for ultimate performance, you might prefer the faster Streaming API. 但是,为了获得最佳性能,您可能更喜欢速度更快的 Streaming API。

My Question 我的问题

What specific differences make Streaming API faster than traditional API ? 具体哪些差异使得Streaming APItraditional API更快?

The key difference is in the way you're handling the file, as you noticed by yourself with the factory class. 关键区别在于您处理文件的方式,正如您自己注意到的工厂类。

The streaming API is not saving in disk while getting the input stream. 获取输入流时,流API不会保存在磁盘中。 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. 之后,您可以使用bufferedInputstream,字节数组或类似方法将数据保存到磁盘。

EDIT: The handler when you open the stream ( fileItemStreamElement. openStream () ) is a common InputStream instance. 编辑:打开流时的处理程序( fileItemStreamElement.openStream ())是一个常见的InputStream实例。 So, the answer to your "what if it's a big file" is something like this Memory issues with InputStream in Java 所以,你的“如果它是一个大文件”的答案就像这样的内存问题与Java中的InputStream

EDIT: The streaming API should not save to disk OR save in memory. 编辑:流API不应保存到磁盘或保存在内存中。 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. 这应该更快,至少因为它没有被复制两次,一次从浏览器复制到磁盘/内存,然后再从磁盘/内存复制到你保存它的地方。

The traditional API, which is described in the User Guide, assumes, that file items must be stored somewhere, before they are actually accessable by the user. “用户指南”中描述的传统API假定文件项必须存储在某个位置,然后才能由用户实际访问。 This approach is convenient, because it allows easy access to an items contents. 这种方法很方便,因为它可以轻松访问项目内容。 On the other hand, it is memory and time consuming. 另一方面,它是内存和耗时。

http://commons.apache.org/fileupload/streaming.html http://commons.apache.org/fileupload/streaming.html

The streaming API should not save to disk OR save in memory. 流API不应保存到磁盘或保存在内存中。 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. 这应该更快,至少因为它没有被复制两次,一次从浏览器复制到磁盘/内存,然后再从磁盘/内存复制到你保存它的地方。

Streaming generally refers to a API (like Apache FileUpload or StAX) in which data is transmitted and parsed serially at application run time, often in real time, and often from dynamic sources whose contents are not precisely known beforehand. 流通常是指API(如Apache FileUpload或StAX),其中数据在应用程序运行时通常是实时地串行传输和解析,并且通常来自其内容未事先准确知道的动态源。

Traditional models refer to APIs like (Traditional file handling APIs, DOM API) which provide a lot more detail information about the data. 传统模型指的是(传统文件处理API,DOM API)等API,它们提供了有关数据的更多详细信息。

Like for a FileHandling API Traditional approach assumes that file items must be stored somewhere, before they are actually accessible by the user. 与FileHandling API类似,传统方法假定文件项必须存储在某个地方,然后才能由用户实际访问。 This approach is convenient, because it allows easy access to an items contents. 这种方法很方便,因为它可以轻松访问项目内容。 On the other hand, it is memory and time consuming. 另一方面,它是内存和耗时。

An Streaming API will have a smaller memory footprint and smaller processor requirements and can have higher performance in certain situations. Streaming API将具有更小的内存占用和更小的处理器要求,并且在某些情况下可以具有更高的性能。

It works on the fundamental of "cardboard tube" view of the document you are working with. 它适用于您正在使用的文档的“纸管”视图的基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM