Frage

i am beginner in java and developing one desktop application.

as per requirement i have to capture screen shot at time interval and send on to the server.

for that i have wrote following code

            Rectangle screenRect = new Rectangle(Toolkit
                    .getDefaultToolkit().getScreenSize());          

            //To Store the scrreen shot in buffer.
            BufferedImage capture = new Robot()
                    .createScreenCapture(screenRect);   

            //To Save the image on specific location.
            ImageIO.write(capture, "png", new File(
                    "resources/img/screenshot.png"));
            //To decrees the size of the image
            Thumbnails.of("resources/img/screenshot.png").scale(0.50)
                    .toFile("resources/img/screenshot.png");

 File file = new File("resources/img/screenshot.png");
    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cfBody = new FileBody(file);

    mpEntity.addPart("screenshot", cfBody);

this code run successfully but problem is that when any machine has not permission to write the file in "Program File folder" that time this is not work.

then is there any way to send the image via httpClient without storing in local machine?

War es hilfreich?

Lösung

What suggested by @AdityaKeyal is definitely doable, but it may require some change to your code. I would try to see if you can use a more convenient location to store the temporary image while sending it, and simply removing when the network process ends succesfully.

According to your "Program File folder" it seems you're on a Windows machine (even if you've backslashes in your paths). IMHO You should use the environment variable %TEMP% which points to different locations on different Windows versions, but is the defined location for this kind of temporary data.

If you are on Linux or OSX you can use /tmp to write temporary data. The folder is writable by everyone.

Andere Tipps

You can try to save it into an instance of ByteArrayOutputStream instead of file. Then use this stream to send via http client.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(capture, "png", baos);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top