Java reads a byte array from the memory stream

Source: Internet
Author: User

The binary data is received by the servlet in Java and then the binary data stream is read as a byte array. Start using:
Byte[] bs = new byte[request.getcontentlength ()];
Request.getinputstream (). read (BS);
return BS;
When the amount of data is small, there is no problem, the data volume is too large to read incomplete, and then modify as follows.

/*** Get the data submitted by the customer *@paramRequest *@return */Private byte[] GetData (HttpServletRequest request) {Try {        //byte[] bs = new byte[request.getcontentlength ()]; //Request.getinputstream (). read (BS); //return BS;servletinputstream sis=Request.getinputstream (); Bytearrayoutputstream Swapstream=NewBytearrayoutputstream (); byte[] Buff =New byte[100]; intrc = 0;  while(rc = sis.read (buff, 0,)) > 0) {swapstream.write (buff,0, RC); }        byte[] in2b =Swapstream.tobytearray (); returnin2b; } Catch(IOException e) {LogHelper.logger.error ("Failed to receive data", E); }    return New byte[0];}

Records to facilitate later viewing

Java reads a byte array from the memory stream

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.