TMD,后期慢慢更新下
try { // get the output stream to write to OutputStream outputStream = response.getOutputStream(); response.setContentType("multipart/x-mixed-replace; boundary=--BoundaryString"); response.setHeader("Connection", "close"); response.setHeader("Max-Age", "0"); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "no-cache, private"); response.setHeader("Pragma", "no-cache"); // loop over and send the images while the browser is present and listening, then return File files = new File("D:\My Projects\Monitor\test1"); for (File f : files.listFiles()) { BufferedImage image = ImageIO.read(f); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", baos); byte[] imageData = baos.toByteArray(); outputStream.write(("--BoundaryStringrn" + "Content-type: image/jpegrn" + "Content-Length: " + imageData.length + "rnrn").getBytes()); outputStream.write(imageData); outputStream.write("rnrn".getBytes()); outputStream.flush(); // force sleep to not overwhelm the browser, simulate ~20 FPS // TimeUnit.MILLISECONDS.sleep(1000 / 50); } outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); }