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.