title: Android: Get a file from http and store in SDCard
body: <p>I've followed what is written in many similar questions but there is still a problem</p>
  
  <p>From a jsp I get a pdf, if i go to the URL the browser opens automatically the pdf, jsp page does something like:</p>
  
  <pre><code>//Gets the pdf from the database
  BufferedInputStream bis = new BufferedInputStream(file.getBinaryStream(), buffer);
  ByteArrayOutputStream baos=new ByteArrayOutputStream();
  int readed=0;
  while ((readed=bis.read())!=-1) baos.write(readed);
  bis.close();
  byte[] pdf=baos.toByteArray();
  response.setContentType("application/pdf");
  response.setContentLength(pdf.length);              
  response.getOutputStream().write(pdf, 0, pdf.length);
  </code></pre>
  
  <p>This code works because if we browse to the URL we get the PDF into the browser.</p>
  
  <p>Then in Android I do in an AsyncTask:</p>
  
  <pre><code>InputStream is = null;
  try {
  URL url = new URL(myurl); // &lt;-- this is the same URL tested into browser
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  urlConnection.setRequestMethod("GET");
  urlConnection.setDoOutput(true);
  urlConnection.connect();
  FileOutputStream fos = new FileOutputStream(getWorkingDir()+fileName);
  InputStream inputStream = urlConnection.getInputStream();
  int totalSize = urlConnection.getContentLength(); //&lt;- this seems to be incorrect,     totalSize value is 22 but file is more than 50Kb length
  int downloadedSize = 0;
  byte[] buffer = new byte[1024];
  int bufferLength = 0; //used to store a temporary size of the buffer
  while ( (bufferLength = inputStream.read(buffer)) &gt;=0) {
    fos.write(buffer, 0, bufferLength);
    downloadedSize += bufferLength;
    // at this point downloadedSize is only 2, and next iteration in while exists so a file os size 2bytes is created...
  }
  fos.close();
  </code></pre>
  
  <p>Of course I've the permission to write in SD and use Internet in the AndrodiManifest.xml</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>I've tried directly with URLConnection, getting the InputStream and we get the same, only reading 2 bytes...</p>
  
  <p>Write to external file is working, if I try to write a <code>string.getBytes()</code> to a file it's written.</p>
  
  <p>If we get <code>conn.getResponseCode()</code> it's 200, so it's ok.</p>
  
  <p>The same .jsp can according to parameters return a list of documents (in JSON) or a PDF if we provide his database ID, if we get the list of pdf, it works, in this case it's readed like:</p>
  
  <pre><code>reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  stringBuilder = new StringBuilder();
  String line = null;
  while ((line = reader.readLine()) != null) {
      stringBuilder.append(line); 
  }
  </code></pre>
  
  <p>Any idea why is not working when it tries to get the binary pdf file?</p>
  
  <p>Where is the failure?</p>
  
  <p>Thanks for your expertice...</p>