code: public static void downloadOSMFile(File file, Envelope geometryEnvelope) throws IOException {
          HttpURLConnection urlCon = (HttpURLConnection) createOsmUrl(geometryEnvelope).openConnection();
          urlCon.setRequestMethod("GET");
          urlCon.connect();
          switch (urlCon.getResponseCode()) {
              case 400:
                  throw new IOException("Error : Cannot query the OSM API with the following bounding box");
              case 509:
                  throw new IOException("Error: You have downloaded too much data. Please try again later");
              default:
                  InputStream in = urlCon.getInputStream();
                  OutputStream out = new FileOutputStream(file);
                  try {
                      byte[] data = new byte[4096];
                      while (true) {
                          int numBytes = in.read(data);
                          if (numBytes == -1) {
                              break;
                          }
                          out.write(data, 0, numBytes);
                      }
                  } finally {
                      out.close();
                      in.close();
                  }       break;
          }
      }
docs: Download OSM file from the official server
  
  @param file
  @param geometryEnvelope
  @throws IOException