确定输入流的大小
我目前的情况是:我必须读取一个文件并将内容放入InputStream
中。 之后,我需要将 InputStream 的内容放入字节数组中,这需要(据我所知)InputStream 的大小。 有任何想法吗?
根据要求,我将显示我从上传的文件创建的输入流
InputStream uploadedStream = null;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
java.util.List items = upload.parseRequest(request);
java.util.Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
uploadedStream = item.getInputStream();
//CHANGE uploadedStreambyte = item.get()
}
}
该请求是一个 HttpServletRequest 对象,它类似于 FileItemFactory 和 ServletFileUpload code> 来自 Apache Commons FileUpload 包。
My current situation is: I have to read a file and put the contents into InputStream
. Afterwards I need to place the contents of the InputStream
into a byte array which requires (as far as I know) the size of the InputStream
. Any ideas?
As requested, I will show the input stream that I am creating from an uploaded file
InputStream uploadedStream = null;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
java.util.List items = upload.parseRequest(request);
java.util.Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
uploadedStream = item.getInputStream();
//CHANGE uploadedStreambyte = item.get()
}
}
The request is a HttpServletRequest
object, which is like the FileItemFactory
and ServletFileUpload
is from the Apache Commons FileUpload package.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这是一个非常古老的线程,但当我用谷歌搜索这个问题时,它仍然是第一个弹出的东西。 所以我只想补充一点:
为我工作。 而且比这里的其他答案简单得多。
警告 此解决方案无法提供有关流总大小的可靠结果。 除了 JavaDoc 之外:
This is a REALLY old thread, but it was still the first thing to pop up when I googled the issue. So I just wanted to add this:
Worked for me. And MUCH simpler than the other answers here.
Warning This solution does not provide reliable results regarding the total size of a stream. Except from the JavaDoc:
我会读入 ByteArrayOutputStream 然后调用toByteArray() 获取结果字节数组。 您不需要提前定义大小(尽管如果您知道的话这可能是一种优化。在很多情况下您不会)
I would read into a ByteArrayOutputStream and then call toByteArray() to get the resultant byte array. You don't need to define the size in advance (although it's possibly an optimisation if you know it. In many cases you won't)
如果不读取流,则无法确定流中的数据量; 但是,您可以询问文件的大小:
http://java.sun.com/javase/6/docs/api/java/io/File.html#length()
如果不可能,您可以写入您想要的字节从输入流读取到 ByteArrayOutputStream将根据需要增长。
You can't determine the amount of data in a stream without reading it; you can, however, ask for the size of a file:
http://java.sun.com/javase/6/docs/api/java/io/File.html#length()
If that isn't possible, you can write the bytes you read from the input stream to a ByteArrayOutputStream which will grow as required.
我只是想补充一点,Apache Commons IO 具有流支持实用程序来执行复制。 (顺便说一句,将文件放入输入流是什么意思?您可以向我们展示您的代码吗?)
编辑:
好的,您想如何处理该项目的内容?
有一个
item.get()
它返回字节数组中的整个内容。Edit2
item.getSize()
将返回上传的文件大小。I just wanted to add, Apache Commons IO has stream support utilities to perform the copy. (Btw, what do you mean by placing the file into an inputstream? Can you show us your code?)
Edit:
Okay, what do you want to do with the contents of the item?
There is an
item.get()
which returns the entire thing in a byte array.Edit2
item.getSize()
will return the uploaded file size.对于输入流
对于可选< 多部分文件>
For InputStream
For Optional < MultipartFile >
下面的函数应该适用于任何
InputStream
。 正如其他答案所暗示的那样,如果不通读它,您就无法可靠地找到InputStream
的长度,但与其他答案不同的是,您不应该尝试保存整个流通过读入 ByteArrayOutputStream 来存储在内存中,也没有任何理由这样做。 理想情况下,您应该依赖其他 API 来获取流大小,而不是读取流,例如使用File
API 获取文件的大小。为
chunkSize
选择一个适合InputStream
类型的合理值。 例如,从磁盘读取数据时,如果chunkSize
的值太小,效率会很低。The function below should work with any
InputStream
. As other answers have hinted, you can't reliably find the length of anInputStream
without reading through it, but unlike other answers, you should not attempt to hold the entire stream in memory by reading into aByteArrayOutputStream
, nor is there any reason to. Instead of reading the stream, you should ideally rely on other API for stream sizes, for example getting the size of a file using theFile
API.Choose a reasonable value for
chunkSize
appropriate to the kind ofInputStream
. E.g. reading from disk it would not be efficient to have too small a value forchunkSize
.您可以使用 Utils.java 的 getBytes(inputStream) 获取 InputStream 的大小,请检查以下链接
从输入流获取字节
you can get the size of InputStream using getBytes(inputStream) of Utils.java check this following link
Get Bytes from Inputstream
当显式处理 ByteArrayInputStream 时,与本页上的一些注释相反,您可以使用 .available() 函数来获取大小。 只需在开始阅读之前执行此操作即可。
来自 JavaDocs:
https://docs.oracle。 com/javase/7/docs/api/java/io/ByteArrayInputStream.html#available()
When explicitly dealing with a
ByteArrayInputStream
then contrary to some of the comments on this page you can use the.available()
function to get the size. Just have to do it before you start reading from it.From the JavaDocs:
https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html#available()
如果您知道您的
InputStream
是FileInputStream
或ByteArrayInputStream
,则可以使用一点反射来获取流大小而无需阅读全部内容。 这是一个示例方法:我确信这可以扩展以支持额外的输入流。
If you know that your
InputStream
is aFileInputStream
or aByteArrayInputStream
, you can use a little reflection to get at the stream size without reading the entire contents. Here's an example method:This could be extended to support additional input streams, I am sure.
如果您需要将数据流式传输到另一个不允许您直接确定大小的对象(例如javax.imageio.ImageIO),那么您可以包装您的InputStream在 内
CountingInputStream
(Apache Commons IO),然后读取大小:If you need to stream the data to another object that doesn't allow you to directly determine the size (e.g.
javax.imageio.ImageIO
), then you can wrap yourInputStream
within aCountingInputStream
(Apache Commons IO), and then read the size:添加到您的 pom.xml:
用于获取内容类型长度(InputStream 文件):
Add to your pom.xml:
Use to get the content type lenght (InputStream file):
如果您想忽略内容并只获取流的长度(以字节为单位),那么您可以这样做(从 Java 11 开始)。
长长度 = inputStream.transferTo(OutputStream.nullOutputStream())
If you want to ignore the contents and just get the length of the stream (in bytes) then you can do this (since Java 11).
long length = inputStream.transferTo(OutputStream.nullOutputStream())
使用这个方法,你只需要传递InputStream
Use this method, you just have to pass the InputStream
int 可用 ()
返回可以从此输入流读取(或跳过)的字节数的估计值,而不会被该输入流的方法的下一次调用所阻塞。 下一次调用可能是同一个线程或另一个线程。 单次读取或跳过这么多字节不会阻塞,但可能会读取或跳过更少的字节。
InputStream - Android SDK | Android 开发者
int available ()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
InputStream - Android SDK | Android Developers