title: Android Uploading large files
body: <p>I am pretty much new to android development, and I am trying to upload a file of size 25 to 50 MB to a web server, and am getting the out of memory error. I am struggling for past 2 days and have no clue, where I am going wrong.</p>
  
  <p>Got any suggestions on where I am going wrong?</p>
  
  <p>The code I am working on is </p>
  
  <pre><code>private FileInputStream fileInputStream = null;
  private int bytesavailable,buffersize,bytesRead ;
  byte buff[];
  int maxBufferSize = 1*1024*1024;
  String server_url ="server_url";
  DataOutputStream dos;
  String Builder response = new String Builder();
  String body = "boundary values";
  String body2 = "Boundary values";
  URL url = null;
      try 
      {
          url = new URL(server_url);
      } 
      catch (MalformedURLException e1) 
      {
          e1.printStackTrace();
      }
  
      HttpURLConnection conn = null;
      try 
      {
          conn = (HttpURLConnection) url.openConnection();
      } 
      catch (IOException e) 
      {
          e.printStackTrace();
      }
      try 
      {
          conn.setRequestMethod("POST");
          conn.setDoInput(true);
          conn.setDoOutput(true);
          conn.setRequestProperty("Connection","Keep-Alive");
          conn.setRequestProperty("Content-Type","multipart/form-data; boundary=A300x");
          conn.connect();
          dos = new DataOutputStream(conn.getOutputStream());
          dos.writeBytes(body);
          File inputfile = new File(sourceFile);
          fileInputStream = new FileInputStream(inputfile);
  
  
          bytesavailable = fileInputStream.available();
          buffersize = Math.min(bytesavailable, maxBufferSize);
          buff = new byte[buffersize];
          bytesRead = fileInputStream.read(buff, 0, buffersize);
  
  
              while (bytesRead &gt; 0) 
              {
  
                  dos.write(buff, 0, buffersize);
                  dos.flush();
                  bytesavailable = fileInputStream.available();
                  buffersize = Math.min(bytesavailable, maxBufferSize);
                  bytesRead = fileInputStream.read(buff, 0, buffersize);
              }
          fileInputStream.close();
          dos.write("\r\n".getBytes());
          dos.write(body2.getBytes());
          dos.flush();
          dos.close();
  } 
      catch (ProtocolException e) 
      {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      int iresponse = 0;
      try 
      {
          iresponse = conn.getResponseCode();
      } 
      catch (IOException e) 
      {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      //printStream(conn.getInputStream());
  
      if (iresponse == HttpURLConnection.HTTP_OK) 
      {
  
          BufferedReader input = null;
          try 
          {
              input = new BufferedReader(new InputStreamReader(conn.getInputStream()), 8192);
          } 
          catch (IOException e1) 
          {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  
          String line = null;
          try 
          {
              while ((line = input.readLine()) != null)
                  response.append(line);
          } 
          catch (IOException e) 
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          try 
          {
              input.close();
          } 
          catch (IOException e) 
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  
      return response.toString();
  </code></pre>