java - Applet - Unable to write file -
i'm trying write sample file applet not working. below code.
applet
public class pasteimageapplet extends japplet { clipboard clipboard; toolkit toolkit; jlabel lbl; public string getclipboardimageurl(string server) { lbl.settext("pasting image"); string url = ""; try { dataflavor dataflavor = dataflavor.imageflavor; system.out.println(dataflavor.getdefaultrepresentationclass()); object object = null; try { object = clipboard.getcontents(null) .gettransferdata(dataflavor); joptionpane.showmessagedialog(null,"image found."); try { writer output = null; string text = "test write file"; file file = new file("write.txt"); output = new bufferedwriter(new filewriter(file)); output.write(text); output.close(); } catch(exception ex) { joptionpane.showmessagedialog(null,"error writing file"+ex); return "" ; } //return ""; } catch (exception e) { joptionpane.showmessagedialog(null, "no image found."); return ""; } } catch (exception e) { joptionpane.showmessagedialog(null, "error."+e); return ""; } return url; } public void init() { lbl = new jlabel(""); lbl.settext("applet started"); add(lbl); toolkit = toolkit.getdefaulttoolkit(); clipboard = toolkit.getsystemclipboard(); } }
html
<html> <head> <title>clipboard image demo</title> <script type="text/javascript"> function loadapplet() { // deferred load display text first document.getelementbyid("applet").innerhtml = '<object id="paste-image" classid="java:pasteimageapplet.class" type="application/x-java-applet" archive="tst.jar" width="1" height="1"></object>'; } function getimage() { obj = document.getelementbyid('paste-image'); postto = "http://localhost/pasteimageapplet/pasteimageapplet/web/shoot.php"; // change url image = obj.getclipboardimageurl(postto); if (image) { url = "shots/" + image; document.getelementbyid("target").src = url; document.getelementbyid("url").value = document.getelementbyid("target").src; // full path, hack, know ;) document.getelementbyid("container").style.display = ""; } } </script> <body onload="loadapplet();"> <p> copy image data clipboard, accept applet (it accesses clipboard) , click button :-) <a href="http://lassebunk.dk/2009/07/19/using-the-clipboard-to-post-images/">see blog post demo</a> </p> <p> <div id="applet"></div> <input type="button" value="paste it!" onclick="getimage();"> </p> <div id="container" style="display: none;"> <input type="text" id="url" style="width: 700px;"><br /> <iframe id="target" width="700" height="400"></iframe> </div> </body> </html>
i didn't error well. please advice.
that's because applets live there own sandbox, require special permission perform operations, read or write disk of client machine. remember, applets execute within context of client machine, guests , need follow house rules
check out what applets can , cannot do more details
Comments
Post a Comment