简体   繁体   中英

How to pass byte[] array from InputStrem to OutputStream?

I have stored my image as mediumblob in database

In my Image bean class I have store photo property as byte[] like

private byte[] photo;

// getter and setter method for photo

I get image from database using to store in Image bean class

image.setPhoto(resultset.getBinaryStreams(1));

then i get the image in Servlet as:

InputStream input = null;
OutputStream output = null;
try {
    input = new ByteArrayInputStream(image.getPhoto());

    output = // What type of stream should I use here

    byte[] buffer = new byte[10240];
    int length;
    while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }
} finally {
    output.flush();
    input.close();
}

Questions:

  1. WHat should I write at output line to show photo using response.getOutputStream or something else?
  2. Is this method correct or is there any better way?

Your output stream can be that stream returned by response.getOutputStream() directly. Just be sure to flush the stream after writing all the data to it.

If your servlet container isn't buffering output streams, you can consider wrapping the output in a BufferedOutputStream .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM