java - 小程序 - 无法写入文件

标签 java html applet bufferedwriter

我正在尝试从小程序编写示例文件,但无法正常工作。下面是代码。

小程序

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>

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

最佳答案

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

查看What Applets can and cannot do了解更多详情

关于java - 小程序 - 无法写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17779024/

相关文章:

java - 是否可以将 JVM 准备的 jar 文件转换为 Dalvik 兼容的 jar?

Java SpringBoot OpenApi @ApiResponse 显示错误的返回对象

java - 使用 JDBC 执行查询时出现错误 "column not allowed"

javascript - Node.JS Express 到 HTML 数据传输

javascript - 为什么这个 event.url 起作用而不是这个

java - 我们什么时候得到 java.lang.NoSuchMethodError 即使 jar/class 有特定的方法

java - WUT-121 该文件传输已被管理员禁止

java - 如何保存数组或对象以供其他运行使用

android - 相机照片未显示 ionic 3

java - 在jlist中添加图片