0

I have a folder named "images" i want to convert all the images in that folder in to streams and display it in a jsp page is that possible if so tell me how to do it.currently i can convert a single image in to stream using the following code

String filename=request.getParameter("source1");   
String str="C:\\nambi\\"+"new";
InputStream inputStream = new FileInputStream(str);
byte[] bytes = IOUtils.toByteArray(inputStream);
response.setContentType("image/jpeg");
OutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.close();
mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
venkat
  • 1
  • 3
  • why do you want to stream multiple images at a time? what do you want to achieve? – Braj Aug 04 '14 at 10:43
  • i am speaking about converting multiple files from a specific folder int o streams i don't think it matches the one you said??if so can u explain me how? – venkat Aug 04 '14 at 10:44
  • i need to display all the images from a specific folder in a jsp page i want it to achieve it using streams so that i can upload and view the images from anywhere in my network – venkat Aug 04 '14 at 10:46
  • It's explained at [Java Ranch -Multiple Files in an InputStream](http://www.coderanch.com/t/378485/java/java/Multiple-Files-InputStream) along with working example. – Braj Aug 04 '14 at 10:47
  • You want to display all images in JSP page ? – Malatesh Aug 04 '14 at 10:49
  • jsp or html anything is fine – venkat Aug 04 '14 at 10:50
  • Normally, you would do this using one JSP/HTML file, with `img` tags for each image in your folder, and one Servlet (or similar, you could use JSP) that serves images for the URLs found in the `src` attribute of the `img` tags. Are you sure you want multiple images in a single stream? – Harald K Aug 04 '14 at 11:10
  • thank you guys my program is working now – venkat Aug 04 '14 at 12:19

1 Answers1

0

You can use SequenceInputStream.

A SequenceInputStream represents the logical concatenation of other input streams. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams

Here is an example and one more

sample code directly form above link:

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class SequenceInputStreamTest {

    public static void main(String[] args) throws Exception {

        FileInputStream fis1 = new FileInputStream("testfile1.txt");
        FileInputStream fis2 = new FileInputStream("testfile2.txt");
        FileInputStream fis3 = new FileInputStream("testfile3.txt");

        Vector<InputStream> inputStreams = new Vector<InputStream>();
        inputStreams.add(fis1);
        inputStreams.add(fis2);
        inputStreams.add(fis3);

        Enumeration<InputStream> enu = inputStreams.elements();
        SequenceInputStream sis = new SequenceInputStream(enu);

        int oneByte;
        while ((oneByte = sis.read()) != -1) {
            System.out.write(oneByte);
        }
        System.out.flush();
    }
}
Braj
  • 46,415
  • 5
  • 60
  • 76