确定 InputStream 的大小 [英] Determine the size of an InputStream

查看:118
本文介绍了确定 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 对象,就像 FileItemFactoryServletFileUpload 来自 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屋!

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