0

InputStreamReader を使用して圧縮画像を転送します。InflaterInputStream は画像の解凍に使用されます

InputStreamReader infis =
   new InputStreamReader(
      new InflaterInputStream( download.getInputStream()), "UTF8" );
do {
   buffer.append(" ");
   buffer.append(infis.read());
} while((byte)buffer.charAt(buffer.length()-1) != -1);

ただし、非ラテン文字はすべて「?」になります。画像が壊れていますhttp://s019.radikal.ru/i602/1205/7c/9df90800fba5.gif

圧縮されていない画像の転送では、BufferedReader を使用していますが、すべて正常に動作しています

BufferedReader is =
   new BufferedReader(
      new InputStreamReader( download.getInputStream()));
4

1 に答える 1

5

リーダー/ライター クラスは、テキスト (文字ベース) の入出力を処理するように設計されています。

圧縮されたイメージはバイナリであり、バイナリ データを転送するには、InputStream/OutputStream または nio クラスのいずれかを使用する必要があります。

InputStream/OutputStream を使用した例を以下に示します。この例では、受信したデータをローカル ファイルに保存します。

    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {

        bis = new BufferedInputStream(download.getInputStream());
        bos = new BufferedOutputStream(new FileOutputStream("c:\\mylocalfile.gif"));

        int i;
        // read byte by byte until end of stream
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
    } finally {
        if (bis != null)
            try {
                bis.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        if (bos != null)
            try {
                bos.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
    }
于 2012-05-24T18:40:42.193 に答える