0

バイト配列を16進文字列に変換しようとしました。しかし、メモリ不足エラーの例外が発生します。これについてはまったくわかりませんでした。

        private String byteArrayToHexString(byte[] array) {
        StringBuffer hexString = new StringBuffer();
        for (byte b : array) {
          int intVal = b & 0xff;
          if (intVal < 0x10)
            hexString.append("0");
          hexString.append(Integer.toHexString(intVal));
        }
        return hexString.toString();

助けてくれてありがとう

4

2 に答える 2

0

これが例です。基本的に、最初にWebサーバーに接続してから、出力ストリームで画像を直接hexstringに変換して、バイトがサーバーに直接送信されるようにします。最初に画像全体を巨大な文字列に変換してから、巨大な文字列に変換する必要はありません。それをサーバーにプッシュします。

byte[] array; // This is your byte array containing the image
URL url = new URL("http://yourwebserver.com/image-upload-or-whatever");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
    urlConnection.setDoOutput(true);
    urlConnection.setChunkedStreamingMode(0);

    OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
    // Now you have an output stream that is connected to the webserver that you can
    //  write the content into
    for (byte b : array) {
        // Get the ASCII character for the first and second digits of the byte
        int firstDigit = ((b >> 4) & 0xF) + '0';
        int nextDigit = (b & 0xF) + '0';
        out.write(firstDigit);
        out.write(nextDigit);
    }
    out.flush(); // ensure all data in the stream is sent
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    readStream(in); // Read any response
} finally {
    urlConnection.disconnect();
}

私はこのコードを試していませんが、うまくいけばあなたはポイントを得るでしょう。

于 2012-05-15T14:59:19.727 に答える
0

送信前に変換しない-送信中に変換する

于 2012-05-15T14:55:32.490 に答える