Using Java HttpUrlConnection to download a PDF file,buffer set to 1024 bytes -- Wrong,but 8 is ok -


I want to use Java (Hispanic Link Connection) to download a PDF file, but there is some puzzle, The core code is:

  BufferedInputStream inputstream = new BufferedInputStream (httpConn.getInputStream ()); OutputStream Output = New File Optical Stream ("F: /httpclient-tutorial.pdf"); Byte [] infoBytes = new byte [buffer]; While (inputstream.read (infoBytes)! = -1) {output.write (infoBytes, 0, buffer); }   

Question: When buffer is 1024, then the PDF file is more than the actual, but the buffer is 8, so it's okay. I do not know why this is? Does anyone know about this?

You are writing without any condition on the buffer on each walk You should only write as many bytes as you've read:

  int bytesRead; While ((BytesRed = inputstream.red (InfoBytes)) = -1) {Output. Written (InfoByte, 0, Bytes Reid); }    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -