Click to See Complete Forum and Search --> : File access with a java applet


DJRobThaMan
09-02-2009, 12:32 PM
Hi,

I've been doing much research on how to access files on a client's file system from a java applet and all the results I've found don't seem to get me anywhere. My applet runs correctly when I run it straight out of Eclipse and here is the code:


import java.applet.*;
import java.io.*;

public class uploader extends Applet
{
public String[][] listfiles(String loc)
{
File folder = new File(loc);
String[] contents = folder.list();
String[][] fullcontent = new String[contents.length + 1][2];
int i = 0;
File temp;
fullcontent[fullcontent.length - 1][0] = "This worked";

while(i < contents.length)
{
temp = new File(folder + contents[i]);
fullcontent[i][0] = contents[i];

if(temp.isDirectory())
{
fullcontent[i][1] = "dir";
}
else
{
fullcontent[i][1] = "file";
}

i++;
}

return fullcontent;
}

public void init()
{
}
}


It's fairly straightforward. The code reads the contents of any given folder on a client's filesystem and returns their filenames. I'm trying to use this applet to upload multiple files or even folders to a server. What I'd like to do with this is for the java applet to be a sort of bridge between the client's file and the php that will then upload all the files. I'm not sure if it will work the way I want it to but first I have to figure out how to get permission to view the user files.

I've tried various things that involve using the keytool and jarsigner commands and up to now cannot get it to work.

Essentially, this applet is embedded in a web page with the following contents:


<?php
$home = $_SERVER['DOCUMENT_ROOT'];
if(strripos($home, '/') != strlen($home) - 1){ $home .= '/'; }
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Add a New Project | SoS
</title>
<link rel="stylesheet" type="text/css" href="/sos.css" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!--<object classid="clsid:CAFEEFAC-0016-0000-0000-ABCDEFFEDCBA" height="400" width="400">
<param name="codebase" value="main.class" />
</object>
<embed name="jav" code="uploader.class" width="400" height="400" type="application/x-java-applet;version=1.6" />-->
<applet name="jav" code="uploader.class" archive="uploader.jar" width="100" height="100"></applet>
<script type="text/javascript">
<!--
var test = document.jav.listfiles("/");
alert(test);
-->
</script>
</body>
</html>


I know the applet tag has been deprecated but I was a bit desperate. I swear I had an embed tag in there and I'll put it back. But with both the applet tag and the embed tag I get the same error which is:

Error: uncaught exception: Error calling method on NPObject! [plugin exception: java.security.AccessControlException: access denied (java.io.FilePermission \ read)].

I know file access can be gained because it's done all over the place (facebook is one example). Anybody know how I can modify or add to my current set-up to fix that?

Many thanks

DrYap
09-12-2009, 01:41 PM
Is that an error from eclipse?

criterion9
09-12-2009, 06:09 PM
You have to have signed the JAR or its security sandbox will not allow local file system access.

DJRobThaMan
09-15-2009, 10:18 AM
criterion9. I've tried to sign the applet and found quite a few sites with instructions on how to do so. Everything I try doesn't seem to make any difference though. Could you explain how to sign it or send a link to some instructions?

Thanks.

DrYap
09-15-2009, 11:37 AM
Here (http://www.developer.com/java/data/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm) is a tutorial about loading a text file with a signed applet. Hope it helps :)