// See what content type we're receiving
String contentType = req.getContentType();
Image image = null;
// An "image/*" content type means to expect the image as an encoded
// byte stream
if (contentType != null && contentType.startsWith("image")) {
// Receive the image bytes as shown in Chapter 6
}
// A "java-internal/image-key" content type means to expect a key
else if ("java-internal/image-key".equals(contentType)) {
// Read the first line of content to get the key
String key = req.getReader().readLine();
// Retrieve the Image stored under that key
image = (Image)System.getProperties().get(key);
// Always remove the Image, to avoid a memory leak
System.getProperties().remove(key);
}
// Other content types cannot be handled
else {
throw new ServletException("Incoming content type must be " +
"\"image/*\" or \"java-internal/image-key\"");
}
// Proceed to use the image as appropriate...
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("Received the image: " + image);