|
Saving Images This java program can save images in JPG, PNG or BMP format, using the "Save Image" option. The default format is PNG. Choose a filename that ends with ".bmp", ".jpg", or ".png" to pick the format. JPG compression sacrifices image quality for small size. BMP preserves the original image quality with no compression. PNG compresses images somewhat less than JPG but retains the original image quality. To save the images, the java SecurityManager must grant file-writing permission to this program. You can arrange this with the Sun Policy Tool utility (you will need system administrator priveleges). Below is a brief explanation of how to grant file writing permission, but you may also want to check out http://java.sun.com/security/index.html and read about Policy Permissions and the use of the Policy Tool. The ScenarioThe following scenario describes granting file writing permission to a java program on a Windows system. Suppose we have a java app named "SomeJavaApp" which can save image files. The app is packaged in a jar archive file titled "SomeJavaApp.jar." When we run the java app as an application (see javaapp.html for help running java applications) and try to save an image file, we get a permission-denied exception message. The default behavior of the java Security Manager denies java programs the permission to write files to the local file system in order to prevent hostile apps from doing harm. In order to change this default behavior, we will use the Sun Policy Tool to grant our java app permission to write image files to our system. Using the Policy Tool to Grant a Permission
First of all, you'll need to locate the policytool.exe file in order to launch it. Many Windows
systems will have more than one policytool.exe file, so be sure to find the one that comes with
your java runtime software, which should have a location resembling :
The Policy Tool will enable us to grant this file-writing permission to a CodeBase (a location
where java software resides), and to specify a Target Name (which restricts which files may
be written by the java software in the CodeBase). For this example, assume the "SomeJavaApp.jar" file is
located in the directory "C:\SomeJavaApp\", residing just below the C: drive. Run the Sun policytool program
by typing "policytool" on the command line (or by double-clicking the policytool.exe file).
Now add a policy : press Add Policy and the Policy Entry window will appear.
file:/C:/SomeJavaApp/SomeJavaApp.jar (note the forward slashes). Next press Add Permission to get the Permissions window. ![]() Use the Permission dropdown box to select "FilePermission". Next use the Actions dropdown box to select "write". Write permission includes file-creation permission. Now we need a target. Target Name
The only item provided in the Target Name dropdown box is the special token "<<ALL FILES>>".
If you choose this as a target, the java software in the CodeBase will be permitted to write
to any file in any location! You can restrict write permission to a single file by
typing the path to that file in the target Name field. In our case, we might want to save
multiple files, but restrict the writing to a single directory. We can use wildcard syntax
(see the Sun documentation) to target all files in a single directory. In the target Name field,
you would type in : C:\SomeJavaApp\* to allow the java software in the CodeBase to write any files in the "C:\SomeJavaApp\" directory. Replacing the asterisk character with a dash ("C:\SomeJavaApp\-") allows file writing in the "C:\SomeJavaApp\" directory and in all of its subdirectories. Did it Work? Press "OK" and "Done" to accept the entries made in the Permission and Policy Entry windows. Then be sure to save the changes to the policy file using File->Save. Hopefully it worked, but if not, make sure you properly saved the user policy file and typed everything in correctly. You can edit or remove the policies in the policy file as needed with the Policy Tool. |