Is it possible to read/write a file from an applet - java

I've trying to read and write a file from an applet so if I run the applet from my appletviewer it is possible for me to read/write a file but when I try to do it from my browser I get a security exception. Any idea how to do this?

Is it possible to read/write a file from an applet
Yes, but you need to sign the applet and get approval from the user.
Some useful links:
How can an Applet read files on the local file system (from coderanch.com)
Essentials, Part 1, Lesson 6: File Access and Permissions (from oracle)
Chapter 10: Signed Applets (from oracle)

A sand-boxed JWS app. can read and write to files on the local file system using the JNLP API services. Here is a demo. of the JNLP API file services. The downloadable Zip file contains the source, launch file and build script.

it's actually not that difficult. You can self sign an applet and then it will be able to read/write to/from the local file system outside the sandbox constraints. take look at the link below. it will show you how to sign your applet using the keytool and the example actually does read/write to the filesystem. no need to muck about the policy files and such.
Note that as #aioobe mentioned, the user will be prompted to accept the certificate though.

Related

Using FileChoosers, JNLP API's under Java Web Start to choose files

I'm using the following link to access JNLP API's:
Using Filechooser
The project: JWSFileChooserDemo
Basically I want an Open and Save dialog box to get "text" file from user, saving it on server, reading it, processing etc and then showing the same .java file on Website.
For the above 2 lines I've tried:
1. "Integration of Applet with Web Application", this integration works correctly but integration of "Applet .jar file in Web Application" restricts the reading of file.
2. Then I used Servlets to do the same job, but file reading doesn't works therefor me.
3. Now, I'm using JNLP, Java Web Start for the same.
But according to what I've read on the website, the applet should be signed for these file operations. Under NetbeansIDE there is an option to enable WEB START and self-signing an applet. But this doesn't work for me.
I created the same program as mentioned in the link(using javax.jnlp), but after clicking on Open, it is looping towards else statement which shows "User cancelled save request.".
I think, the above error is due applet is not signed properly as when I run the JNLP file given on the link..it asks for following confirmation i.e.
So, should I sign an applet using Project properties->WebStart->Enable WebStart->signing->self-signing by generated key or by signing it using keytools which generated certificate i.e .crt file. Is there any other way to do this?
Is there any other way to do this?
It is possible to use the JNLP API services in an unsigned application. Having said that, Oracle has decided that in a future JRE, unsigned code will not be loaded at all (using the default security preferences).
Note that those two linked pages refer to two different ways to load files.
'Using Filechooser' uses the JFileChooser provided with Swing. An applet/JWS app. that uses a file chooser needs to declare all-permissions in the launch file.
The JWSFileChooserDemo demo. OTOH, uses the JNLP API file services, and would produce a prompt at run-time similar to the one shown. At the moment these do not need to be signed.
As far as 'self signed' goes, Oracle has also put warnings against using that. The publisher of a self-signed app. at the moment, will show as UNKNOWN with extra warnings about accepting code from unknown sources. In the future, the JRE will also automatically reject self-signed code.
For the longer term, the only real option is to supply a digitally signed app. Signed using a verified certificate from a Certification Authority.

Java applet for file upload(no limit).direct upload from local dorectory to server

I want to make a java applet, it will update all the files in my local directory to a web server. There is no limit for files. I am new to making applets.
I have no applet interface.
I have some files in my c drive those should be uploaded directly in to web server.
This should be done automatically.
Because of java applet security restrictions, it is impossible to read the local files, because you don't want an applet uploading sensitive information, do you? If you want this to work, you have to sign the applet, and users have to agree to run the signed applet and give it more permissions.

Write to a local file through JNLP

I have a specific requirement to read a resource from jar file and then write it to local disk. But when I try to write that resource to local disk an exception:
java.io.FilePermission (write) is raised.
I have my jar signed. I have also tried the following solution but it does not work either.
AccessController.doPrivileged(new PrivilegedAction()
Nothing seems to work.
Am I missing a step after signing jar because this is the most common solution available everywhere?
How can I write to a local file without using policy files (because that can't be done on each client machine)?
You can save a file, with user interaction, from an unsigned WebStart application using javax.jnlp.FileSaveService.
Possibly a piece you are missing is marking the aplication as secure in the JNLP file. However, writing a secure application is a little tricky, and I wouldn't recommend it.
The JNLP also needs to specify:
<security>
<all-permissions/>
</security>
See the JNLP File Syntax for details.
I would generally use the FileSaveService as detailed in Tom's answer, but this sounds more like a situation where the 'file' could be a default properties file for configuring the app. The save service is not well suited to that, since the app. has no way to know where the file is stored, or how to access it later. For security reasons, the FileContents object that is provided in place of a File has no methods that return the path.
In that case, extract the details of the config. file & store them using the PersistenceService of the JNLP API. Here is a demo. of the PersistenceService. This service is also available to sand-boxed apps. (no code signing voodoo).

Run a java class file from a webpage on the visitor computer?

I coded a Java program to read and modify a file on the computer. The program is based only on 1 class.
At the moment who want to use it has to run it from terminal, I'm looking on how to insert it on a webpage and make it run on the visitor's computer. It would be fine to have a file chooser (the user will want this modification).
I searched on internet and found Java applets, but I read that they aren't downloaded and executed locally so the program won't work.
How to provide a Java class file from a webpage, for use on the computer of the end-user?
If you really want to download a Java program and run it locally, you should check out Java Web Start.
Briefly, it allows the user to download and run a Java program locally on their machine. It does clever stuff like identify if an updated version is available for download, and will run the cached version if that's the current version.
Here's a tutorial.
..it would be fine to have a file chooser ..
In that case, there are basically the two options as I've outlined in comments throughout this question & the answers. I'll collect them together here:
Digitally sign the applet, get the user to accept the digitally signed code when prompted (before the applet is loaded), then offer a JFileChooser to browse to the file.
If the user has a plugin 2 JRE (chase the links in the JWS info. page for more details), it is possible to deliver the applet to the user unprompted, then leverage the JNLP API to produce a file chooser. The user will be prompted before the dialog appears, this time with a more specific warning.
JWS
For an example, see my applet based GIF animation tool which uses the JNLP API when the user goes to load image frames or save the animated GIF.
That applet is not open source (mostly because of my laziness in not wanting to revisit & tidy the code) but there is a much better example of using the JNLP file services that comes complete with source.
Digital signatures
I don't have any great links about the process of digitally signing code, but note that the 'example of using the JNLP file services' listed above provides one set of signed Jars for 2 different security environments. It also (hopefully obviously) demonstrates how to digitally sign code using Ant (it all happens by invoking the default task in the build.xml).
Applets can modify files locally, if they are signed and the user allows them to.
Read up on signed applets.
I read that they aren't downloaded and excuted locally
Whereever you read that, it is 100% incorrect. Applets are downloaded into the browser and executed at the client host.

How to load signed applet from local disc with full permissions?

I have an application where javascript code is accessing java applet methods that write on local disk (just to note - this is not directly possibly, but there is a workaround for it that enables to do so). Applet is self-signed applet, so users have to allow it to run first.
If applet's codebase is set to "http://..." everything works fine, in FF, Chrome and Opera as well. However, I would like to put this applet on local file system. However, if I use codebase "file://..." (I also tried to not use codebase at all and write directly full jar path to archive) it does not work, applet does not load with Warning-Security pop-up that enable user to allow running the applets.
I only tested it in FF (3.6.1.4) so far, Java version is 1.6.0.21.
Are signed applets limited only for HTTP use? Or is there some workaround?
..is there some workaround?
Perhaps using the JNLP API services in an unsigned applet will work better. I have a demo. of the JNLP FileContents object.
For details on using the JNLP services in an embedded applet see the links to the 'next generation plug-in' in the applet information page.
Applets loaded from the local filesystem are allowed to write files only in, or below, the directory containing the applet. So, if the applet is in C:\MyProjects\MyAppletTest it will be able to read and write files in that directory and its subdirectories, but not in C:\MyProjects. You can override this behavior with a policy files as explained in Quick Tour of Controlling Applets

Categories

Resources