Blob 转 OutputStream

StreamingOutput.java

import java.io.IOException;
import java.io.OutputStream;

import javax.ws.rs.WebApplicationException;

/**
 * A type that may be used as a resource method return value or as the entity
 * in a {@link Response} when the application wishes to stream the output.
 * This is a lightweight alternative to a
 * {@link javax.ws.rs.ext.MessageBodyWriter}.
 *
 * @author Paul Sandoz
 * @author Marc Hadley
 * @see javax.ws.rs.ext.MessageBodyWriter
 * @see javax.ws.rs.core.Response
 * @since 1.0
 */
public interface StreamingOutput {

    /**
     * Called to write the message body.
     *
     * @param output the OutputStream to write to.
     * @throws java.io.IOException if an IO error is encountered
     * @throws javax.ws.rs.WebApplicationException
     *                             if a specific
     *                             HTTP error response needs to be produced. Only effective if thrown prior
     *                             to any bytes being written to output.
     */
    void write(OutputStream output) throws IOException, WebApplicationException;
}

Conversion process

// by patient inquiry Photo ID information 
@Override
 public StreamingOutput getPatientPhotoByPatientId (String patientId) {
     // obtain patient information inside photos and fingerprint information contained blob type of 
    PatInfoNote patInfoNote = strictFindByPrimaryKey (PatInfoNote. Class , patientId, "unspecified find information on patient patientId " );
     IF (patInfoNote.getPatientPhotograph () == null ) {
         the throw  new new NotFoundException (String.format (" photo patient information exists patientId [% S] " , patientId)); 
    } 
    StreamingOutput streamingOutput = null ;
     the try {
         / / conversion process 
        streamingOutput = StringUtils.getStreamingOutputByBlob(patInfoNote.getPatientPhotograph());
    } catch (SystemException e) {
        throw new SystemException("读取患者照片失败");
    }
    return streamingOutput;
}
//-------
public static StreamingOutput getStreamingOutput(final InputStream in) throws IOException {
    StreamingOutput stream = new StreamingOutput() {
        public void write(OutputStream out) throws IOException, WebApplicationException {
            try {
                int read = false;
                byte[] bytes = new byte[1024];

                int readx;
                while((readx = in.read(bytes)) != -1) {
                    out.write(bytes, 0, readx);
                }

            } catch (Exception var4) {
                throw new IOException("文件流读取失败: " + var4.getMessage(), var4);
            }
        }
    };
    return stream;
}

public static StreamingOutput getStreamingOutputByBlob(Blob blob) throws RuntimeException {
    StreamingOutput streamingOutput = null;

    try {
        streamingOutput = getStreamingOutput(blob.getBinaryStream());
        return streamingOutput;
    } catch (Exception var3) {
        throw new RuntimeException("读取数据流失败", var3);
    }
}

 

Guess you like

Origin www.cnblogs.com/ms-grf/p/10938950.html