Applet - Unable to write file

那年仲夏 提交于 2019-12-31 05:17:10

问题


I'm trying to write sample file from applet but is not working. Below is the 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 to 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 this to your URL

                image = obj.getClipboardImageURL(postTo);

                if (image) {
                    url = "shots/" + image;

                    document.getElementById("target").src = url;
                    document.getElementById("url").value = document.getElementById("target").src; // to get full path, hack, I know ;)
                    document.getElementById("container").style.display = "";
                }
            }
        </script>

        <body onload="loadApplet();">

            <p>
                Copy some image data to your clipboard, accept the applet (it only accesses the clipboard) and click the button :-)
                <a href="http://lassebunk.dk/2009/07/19/using-the-clipboard-to-post-images/">See a blog post about this 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 get any error as well. Please advice.


回答1:


That's because applets live there own sandbox, where they require special permission to perform certain operations, like read or write the disk of a client machine. Remember, applets execute within the context of the client machine, they are guests and need to follow the house rules

Check out What Applets can and cannot do for more details




回答2:


An applet cannot establish a File on the server. That is not how servers work. If a server accepts uploads, it must provide specific functionality to enable that and the applet must use that functionality.

As long as the functionality to accept an upload is on the same server, the applet can remain sand-boxed.




回答3:


you can see the console for applets via the (windows) taskbar, you right click the java icon(when applet is running it should appear on the lower right) and right click > open console, you can debug there, this is where the stack trace of an applet goes.



来源:https://stackoverflow.com/questions/17779024/applet-unable-to-write-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!