Heim  >  Fragen und Antworten  >  Hauptteil

Java – Eingabestrom wird in Byte-Array konvertiert und das Array liegt außerhalb der Grenzen

public static byte[] readInputStream(InputStream inStream) löst eine Ausnahme aus {

    try {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }catch (Exception e){
        e.printStackTrace();
        throw new Exception(e);
    }

}

So geht man im Internet damit um. Es besteht die Möglichkeit, die Grenze zu überschreiten, indem man zu Tode schreibt

Ich weiß nicht, ob es eine andere Möglichkeit gibt, damit umzugehen

巴扎黑巴扎黑2713 Tage vor729

Antworte allen(2)Ich werde antworten

  • 阿神

    阿神2017-05-17 10:05:02

    最好的方法是用Apache commons IO的IOUtils.toByteArray(inputStream),一行代码解决。

    Antwort
    0
  • 阿神

    阿神2017-05-17 10:05:02

            int count = 0;
            while (count == 0) {
                count = inStream.available();
            }
            byte[] b = new byte[count];
            inStream.read(b);
            return b;

    Antwort
    0
  • StornierenAntwort