Search code examples
javaclassuploadappletscreenshot

Screen And Upload Java Class Works Locally But Not Online. No Errors Whatsoever. What's Wrong?


I'm a novice, so please don't be too hard on me. The concept is pretty simple, I want my users to be viewing a certain section of my site where a screenshot is snapped on their machine and uploaded back to my site.

I'm not getting any visible errors with the class online, but I'm guessing there's something wrong with the upload part since no screenshot img is put in my account when I host the class. Locally, the path being set below to C:/ like someone helped me with works fine. How do I get it to work on the web?

import java.applet.*;
import java.awt.AWTException;
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.net.*;
import java.io.*;
import javax.imageio.ImageIO;

public class ScreenShot extends Applet {

static boolean captureScreenShot(String uploadPath) 
{
boolean isSuccesful = false;
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture;
try {
URL whatismyip = new URL("http://mysite.com/misc.php?page=showremoteaddr");
BufferedReader in = new BufferedReader(new InputStreamReader(
            whatismyip.openStream()));

String ip = in.readLine(); //you get the IP as a String
capture = new Robot().createScreenCapture(screenRect);
// screen shot image will be save at given path with name "screen.jpeg"
ImageIO.write(capture, "png", new File( uploadPath, ip + ".png")); 
isSuccesful = true;
} catch (AWTException awte) {
awte.printStackTrace();
isSuccesful = false;
}
catch (IOException ioe) {
ioe.printStackTrace();
isSuccesful = false;
}
return isSuccesful;
}

public static void main(String [] args){
    String path = "/var/chroot/home/content/srvu/srvr/www";
    captureScreenShot(path);
}
}

Solution

  • Java applets downloaded from the Internet don't have permission to take screenshots or to access the filesystem. Imagine the enormous security problems if they did! The key to getting these permissions granted is to digitally sign your applet -- a reasonably involved process that may cost money, too. Here is a brief tutorial on the topic.

    Also, applets don't have a main() routine -- or more properly, if you create an applet and give it a main() method, the browser won't call it. The code in your applet never runs! Applets have their own set of entry points; you can learn the basics of creating them here.