I want to give a button in my JSP page, which on click of it should store an image in the filesystem.

Following is the code ScreenCapture.java which is a servlet.
Purpose: To capture the screen of working area and store it in file system.
Problem: - An image file is stored in file system with blank image.

Please help me to fix what is wrong

.

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author RAKESH
 */
public class ScreenCapture extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String inf = "c:\\new1", fileType = "JPG", fileName = null;
        fileName = inf + "." + fileType;
        try {
            try {
                Robot robot = new Robot();
                BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
                ImageIO.write(screenShot, fileType, new File(fileName));
                //captureScreen(fileName);
            } catch (Exception ex) {
                Logger.getLogger(ScreenCapture.class.getName()).log(Level.SEVERE, null, ex);
            }
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ScreenCapture</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ScreenCapture at " + request.getContextPath() + "</h1>");
            out.println("<h2>ScreenCapture stored at " + fileName + "</h2>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    public void captureScreen(String fileName) throws Exception {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(fileName));
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

If you observe captureScreen method is working well if you execute separately as a java file (non-servlet)
only with servlet this is not working.

Member Avatar for LastMitch

If you observe captureScreen method is working well if you execute separately as a java file (non-servlet) only with servlet this is not working.

@rakesh4java

Can you post an error or what line that is not working.

hi , did you get this worked ? am facing same issue . If you have fixed please let me know how to do . Am not facing any error , but not image file too

@Tejaswini, This thread is over 5 years old.
it's no use replying to it.
If you have a question of your own, please ask it in a new Thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.