public static URLConnection postMultipart(URLConnection conn, Map<String, String> params,
      List<Entry<String, InputStream>> files) throws IOException {

    if ((params == null || params.size() == 0) && (files == null || files.size() == 0)) { return conn; }

    conn.setDoOutput(true);

    String boundary = Long.toHexString(System.currentTimeMillis());
    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

    try (NMultiPartOutputStream mos = new NMultiPartOutputStream(conn.getOutputStream(), boundary)) {

      if (params != null) {
        for (Entry<String, String> param : params.entrySet()) {
          multipart(mos, param.getKey(), param.getValue());
        }
      }

      if (files != null) {
        for (Entry<String, InputStream> file : files) {
          multipart(mos, file.getKey(), file.getValue());
        }
      }
    }

    return conn;
  }