import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; public class Example1 { public static void main(String[] args) throws IOException { File outputFile = new File("output.jpg"); ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile); // write image data to the output stream ios.close(); } }
import java.io.IOException; import java.net.Socket; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.awt.image.BufferedImage; public class Example2 { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 8080); ImageOutputStream ios = ImageIO.createImageOutputStream(socket.getOutputStream()); BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); ImageIO.write(image, "jpg", ios); // write image in jpg format to the output stream ios.close(); } }In this example, a network socket is created to send image data to a remote location. The createImageOutputStream method is called to create an ImageOutputStream object that is associated with the output stream of the socket. A BufferedImage is created to hold image data, and the ImageIO.write method is used to write the image data in JPEG format to the output stream. The output stream is then closed.