emptygif
மொழி: தமிழ்

அனைத்து மொழிகளும் ஃப்ரேம்வொர்க்குகளும்

Java standard library (com.sun.net.httpserver) Java

/pixel.gif க்கு வரும் ரெக்வெஸ்ட்களுக்கு empty GIF மூலம் பதிலளிக்கும், Java standard library (com.sun.net.httpserver) க்கான Java ஸ்னிப்பெட். GIF ஸ்டார்ட்அப்பில் ஒருமுறை மட்டுமே டீகோட் செய்யப்படுகிறது.

Java standard library (com.sun.net.httpserver) Java

ஃபைல்
Pixel.java
ரன்
java Pixel.java
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
import java.util.Base64;

public class Pixel {

    private static final byte[] GIF = Base64.getDecoder().decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/pixel.gif", exchange -> {
            // Contexts match by prefix, so check for the exact path.
            if (!exchange.getRequestURI().getPath().equals("/pixel.gif")) {
                exchange.sendResponseHeaders(404, -1);
                exchange.close();
                return;
            }
            exchange.getResponseHeaders().set("Content-Type", "image/gif");
            exchange.getResponseHeaders().set("Cache-Control", "no-store");
            exchange.sendResponseHeaders(200, GIF.length);
            try (var body = exchange.getResponseBody()) {
                body.write(GIF);
            }
        });
        server.start();
    }
}

இந்த ஸ்னிப்பெட்டை ரன் செய்து, அது திருப்பித் தந்த பைட்களை ஒப்பிட்டுப் பார்த்தோம்.

சரிபார்

curl -sI http://localhost:8080/pixel.gif

அதிகாரப்பூர்வ சைட்: Java standard library (com.sun.net.httpserver)

Java இல் மேலும்