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

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

问题描述

我正在尝试从小程序编写示例文件,但没有成功.下面是代码.

小程序

public class PasteImageApplet extends JApplet {剪贴板剪贴板;工具包工具包;JLabel lbl;公共字符串 getClipboardImageURL(字符串服务器){lbl.setText("粘贴图片");字符串 url = "";尝试 {DataFlavor dataFlavor = DataFlavor.imageFlavor;System.out.println(dataFlavor.getDefaultRepresentationClass());对象对象=空;尝试 {object = clipboard.getContents(null).getTransferData(dataFlavor);JOptionPane.showMessageDialog(null,"找到图像.");尝试{写入器输出 = null;String text = "测试写入文件";File file = new File("write.txt");output = new BufferedWriter(new FileWriter(file));输出.写(文本);输出关闭();}捕获(异常前){JOptionPane.showMessageDialog(null,"写入文件时出错"+ex);返回 "" ;}//返回 "";} 捕获(异常 e){JOptionPane.showMessageDialog(null, "没有找到图片.");返回 "";}} 捕获(异常 e){JOptionPane.showMessageDialog(null, "Error."+e);返回 "";}返回网址;}公共无效初始化(){lbl = new JLabel("");lbl.setText("小程序启动");添加(磅);工具包 = Toolkit.getDefaultToolkit();剪贴板 = toolkit.getSystemClipboard();}}

HTML

<头><title>剪贴板图像演示</title><script type="text/javascript">函数加载小程序(){//延迟加载以先显示文本document.getElementById("applet").innerHTML = '<object id="paste-image" classid="java:PasteImageApplet.class" type="application/x-java-applet" archive="tst.jar" width="1" 高度="1"></object>';}函数 getImage() {obj = document.getElementById('粘贴图片');postTo = "http://localhost/PasteImageApplet/PasteImageApplet/Web/shoot.php";//将其更改为您的 URL图像 = obj.getClipboardImageURL(postTo);如果(图像){url =镜头/"+图像;document.getElementById("target").src = url;document.getElementById("url").value = document.getElementById("target").src;//获取完整路径,hack,我知道;)document.getElementById("container").style.display = "";}}<body onload="loadApplet();"><p>将一些图像数据复制到剪贴板,接受小程序(它只访问剪贴板)并单击按钮 :-)<a href="http://lassebunk.dk/2009/07/19/using-the-clipboard-to-post-images/">查看有关此演示的博客文章</a></p><p><div id="applet"></div><input type="button" value="粘贴!"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>

我也没有收到任何错误.请指教.

解决方案

那是因为小程序存在于自己的沙箱中,它们需要特殊权限才能执行某些操作,例如读取或写入客户端计算机的磁盘.请记住,applet 是在客户端机器的上下文中执行的,它们是客人,需要遵守内部规则

查看Applet 能做什么和不能做什么了解更多详情

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.

解决方案

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

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

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