确定InputStream的大小 [英] Determine the size of an InputStream
问题描述
我目前的情况是:我必须读取文件并将内容放入 InputStream
。之后我需要将 InputStream
的内容放入一个字节数组中,这个数组需要(据我所知) InputStream的大小
。任何想法?
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()
}
}
请求是 HttpServletRequest
对象,类似于 FileItemFactory
和 ServletFileUpload
来自Apache Commons FileUpload包。
The request is a HttpServletRequest
object, which is like the FileItemFactory
and ServletFileUpload
is from the Apache Commons FileUpload package.
推荐答案
我只是想补充一下,Apache Commons IO有流支持实用程序执行复制。 (顺便问一下,将文件放入输入流是什么意思?你能告诉我们你的代码吗?)
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?)
编辑:
好的,您想对该项目的内容做什么?
有一个 item.get()
返回字节数组中的整个内容。
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()
将返回上传的文件大小。
这篇关于确定InputStream的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!