小程序 - 无法写入文件 [英] Applet - Unable to write file

查看:270
本文介绍了小程序 - 无法写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从写小程序示例文件,但不工作。下面是code。

I'm trying to write sample file from applet but is not working. Below is the code.

小程序

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.

推荐答案

这是因为小程序住在那里自己的沙盒,在那里他们需要特殊的权限执行某些操作,如读取或写入客户机的磁盘。请记住,小程序执行客户机的范围内,他们是客人,并需要遵循内部规则

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

查看什么小应用程序和更不能做细节

这篇关于小程序 - 无法写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆