Download and crop an image direct from a servlet

Tuesday November 13, 2012 ()

This blog is a demonstration on how to download an image and crop them directly just before the servlet writes the image into the response. This is useful in instances where you don't want the downloaded image saved before they are consumed.

The following is that servlet, it takes two parameters, the url of the image, and the image type. Add parameters as your application may require. Please refer to the inline comments for more information.

package info.kahimyang.storm;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "ImageDownloader", urlPatterns = {"/ImageDownloader"})
public class ImageDownloader extends HttpServlet {

    protected void processRequest(HttpServletRequest request, 
            HttpServletResponse response)
            throws ServletException, IOException {

        // Get values of parameters
        String imageType = request.getParameter("type");
        String media = request.getParameter("image_url");

        // Fetch the image
        URL url = new URL(media);
        HttpURLConnection connection = 
                (HttpURLConnection) url.openConnection();
        connection.addRequestProperty("User-Agent", "Mozilla");
        // Read the image into a BufferedImage object
        InputStream stream = connection.getInputStream();
        BufferedImage image =;

        // Get the dimension of the image
        int H = image.getHeight();
        int W = image.getWidth();

        // Crop the image from the top.  This example crops our
        // image 70 pixels from the top
        BufferedImage cropped = image.getSubimage(0, 70, W, H - 70);
        // Write the image into ByteArrayOutputStream
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ImageIO.write(cropped, imageType, byteStream);

        // Update response
        ServletOutputStream out = response.getOutputStream();
        response.setContentType("image/" + imageType);


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    public String getServletInfo() {
        return "Short description";

Note that this servlet uses WebServlet annotation in line 17 above to declare the servlet instead of updating the web.xml descriptor.

From your JSF or HTML page call the servlet similar to shown below:

<!-- storm below should be your application name -->
<img src="/storm/ImageDownloader?image_url=" />

That's it Good Luck.


Comments (Download and crop an image direct from a servlet )