简体   繁体   English

确定 InputStream 的大小

[英]Determine the size of an InputStream

My current situation is: I have to read a file and put the contents into InputStream .我目前的情况是:我必须读取一个文件并将内容放入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 .之后,我需要将InputStream的内容放入一个字节数组中,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,它类似于FileItemFactory ,而ServletFileUpload来自 Apache Commons FileUpload 包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

If you need to stream the data to another object that doesn't allow you to directly determine the size (eg javax.imageio.ImageIO ), then you can wrap your InputStream within a CountingInputStream (Apache Commons IO), and then read the size:如果您需要将数据流式传输到另一个不允许您直接确定大小的对象(例如javax.imageio.ImageIO ),那么您可以将您的InputStream包装在一个CountingInputStream (Apache Commons IO)中,然后读取大小:

CountingInputStream countingInputStream = new CountingInputStream(inputStream);
// ... process the whole stream ...
int size = countingInputStream.getCount();

Add to your pom.xml:添加到您的 pom.xml:

  <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
  </dependency>

Use to get the content type lenght (InputStream file):用于获取内容类型长度(InputStream 文件):

IOUtils.toByteArray(file).length

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

My current situation is: I have to read a file and put the contents into InputStream .我当前的情况是:我必须读取一个文件并将其内容放入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 .然后,我需要将InputStream的内容放入一个字节数组,该数组需要(据我所知) 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.该请求是一个HttpServletRequest对象,类似于FileItemFactoryServletFileUpload来自Apache Commons FileUpload包。

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

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