There are several ways to convert an InputStream to a String in Java. Here are a few examples:

Using BufferedReader:

public static String convertStreamToString(InputStream is) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

Using BufferedReader and Stream API:

public static String convertInputStreamToString(InputStream inputStream) throws IOException {
    try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream))) {
        return buffer.lines().collect(Collectors.joining("\n"));
    }
}

Using Scanner and nextLine method:

public static String convertStreamToString(InputStream is) {
    Scanner s = new Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.nextLine() : "";
}

Using ByteArrayOutputStream:

public static String convertStreamToString(InputStream is) throws IOException {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    }
    return result.toString("UTF-8");
}

In Java9 solution:

public static String convertStreamToString(InputStream is) throws IOException {
    return new String(is.readAllBytes(), StandardCharsets.UTF_8);
   
}

Thanks for reading