Difference between ServletInputStream and FileInputStream ServletInputStream cannot get data Solution

Source: Internet
Author: User

Recently encountered problems in the project:

1: implements file upload and download. If Tomcat and HttpServlet are used, we need to obtain the uploaded byte stream ServletInputStream. The following describes the notes for using ServletInputStream.

(1) The data in ServletInputStream must be read from the cache.

ServletInputStream = request. getInputStream ();
Byte [] a = new byte [ServletInputStream. available ()];
ServletInputStream. read ();

These codes seem to be okay. In fact, they cannot read data because ServletInputStream is optimized by Tomcat and ServletInputStream. available () cannot get the data length. In this way, we get the number of bytes. Let's take a look at FileInputStream.

InputStream = new FileInputStream ();
Byte [] a = new byte [InputStream. available ()];
ServletInputStream. read ();

This code can read data. You can see the difference.


2: to retrieve data from the Servlet, we need to use the while loop to retrieve data from the cache.

Note: The server uses Tomcat. Other servers do not know if this is the case.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.