Apache commons fileupload“Streaming API” [英] Apache commons fileupload "Streaming API"

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

问题描述

我引用了Apache Commons Page for Commons FileUpload


commons fileupload
库的传统API。传统的API是一个方便的方法。但是,对于
的最终性能,您可能更愿意使用更快的Streaming API。

我的问题

具体区别是什么 Streaming API API

解决方案

关键的区别在于您处理文件的方式,由工厂类自己注意到。

在获取输入流时,流式API不保存在磁盘中。最后,你将能够更快地处理文件(在临时内存上花费)......但是想法是避免将二进制文件保存在磁盘中,除非你确实需要/需要。



之后,您可以将数据保存到磁盘,当然也可以使用bufferedinputstream,byte数组或类似的方法。



编辑:处理程序,当你打开流(fileItemStreamElement。 openStream ())是一个常见的InputStream实例。所以,如果这是一个大文件的答案就像这样内存问题在Java中使用InputStream

编辑:流API不应该保存到磁盘或保存在内存中。它只是提供一个可以读取的流,将文件复制到任何你想要的地方。这是避免有一个临时目录,也避免分配足够的内存来保存该文件的方法。这应该是更快,至少是因为它不被复制两次,一次从浏览器到磁盘/内存,然后再次从磁盘/内存到任何你保存它的地方。

I quote from Apache Commons Page for Commons FileUpload

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.

My Question

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.

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.

EDIT: 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

EDIT: 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 commons fileupload“Streaming API”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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