ブロブ转のOutputStream

StreamingOutput.java

インポートにjava.io.IOException;
インポートにjava.io.OutputStream; 

輸入javax.ws.rs.WebApplicationException。

/ ** 
 *リソースのメソッドの戻り値として、またはエンティティとして使用することができるタイプ
 {で* @link アプリケーションが出力をストリーミングしたいレスポンス}。
 *これは、軽量の代替物である
 * { @link javax.ws.rs.ext.MessageBodyWriter}。
 * 
 * @author ポール・サンド
 * @author マルク・ハドレー
 * @see javax.ws.rs.ext.MessageBodyWriter 
 * @see javax.ws.rs.core.Response 
 *@since 1.0
  * / 
パブリック・ インタフェースStreamingOutput { 

    / ** 
     *メッセージ本文を書くために呼び出されます。
     * 
     * @paramの出力に書き込むためのOutputStream。
     * @throws にjava.io.IOException IOエラーが発生した場合
     * @throws javax.ws.rs.WebApplicationException 
     特定の場合には*を
     * HTTPエラー応答を生成する必要があります。唯一の効果的な前投げた場合
     、出力に書き込まれる任意のバイトに*。
     * / 
    無効書き込み(OutputStreamの出力)スローIOExceptionが、WebApplicationException。
}

変換プロセス

// 患者照会写真ID情報によって
@Override
 公共StreamingOutput getPatientPhotoByPatientId(文字列patientId){
     // のBLOB型に含まれる写真や指紋情報の内部で患者情報を取得 
    PatInfoNote patInfoNote = strictFindByPrimaryKey(PatInfoNote。クラス、patientId、「患者に関する情報を、不特定見つけるをpatientId " );
     IF(patInfoNote.getPatientPhotograph()== NULL ){
         スロー 新しい新しい NotFoundException(String.Formatの("写真患者情報がpatientId [%のS]」が存在する、patientIdを)); 
    } 
    StreamingOutput streamingOutput = NULL ;
     試み{
         / / 変換処理 
        streamingOutput =StringUtils.getStreamingOutputByBlob(patInfoNote.getPatientPhotograph())。
    } キャッチ(のSystemException電子){
         スロー 新しい( "读取患者照片失败"のSystemExceptionを
    } 
    戻りstreamingOutput。
} 
// ------- 
パブリック 静的 StreamingOutput getStreamingOutput(最終的に入力ストリーム)がスローIOExceptionが{ 
    StreamingOutputストリーム = 新しいStreamingOutput(){
         公共 ボイドライト(OutputStreamをアウト)スローにIOException、WebApplicationExceptionを{
             試します{
                 INT =読み取るバイト []バイト= 新しい バイト [1024 ]; 

                int型READX;
                一方、(!(READX = in.read(バイト))= -1 ){ 
                    out.write(バイト 0 、READX)。
                } 

            } キャッチ(例外VAR4){
                 スロー 新しい IOExceptionが( "文件流读取失败:" + var4.getMessage()、VAR4)。
            } 
        } 
    }。
    リターンストリーム; 
} 

公共の 静的StreamingOutput getStreamingOutputByBlob(BLOBブロブ)がスローのRuntimeExceptionを{ 
    StreamingOutput streamingOutput = NULL ; 

    試す{ 
        streamingOutput = getStreamingOutput(blob.getBinaryStream())。
        返すstreamingOutputを。
    } キャッチ(例外VAR3){
         スロー 新規のRuntimeException( "读取数据流失败" 、VAR3)を、
    } 
}

 

おすすめ

転載: www.cnblogs.com/ms-grf/p/10938950.html
おすすめ