简体   繁体   English

小程序 - 无法写入文件

[英]Applet - Unable to write file

I'm trying to write sample file from applet but is not working. 我正在尝试从applet编写示例文件,但无法正常工作。 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

<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. 这是因为applet存在于自己的沙盒中,在那里它们需要特殊权限才能执行某些操作,例如读取或写入客户端计算机的磁盘。 Remember, applets execute within the context of the client machine, they are guests and need to follow the house rules 请记住,applet在客户端计算机的上下文中执行,它们是guest虚拟机,需要遵循house规则

Check out What Applets can and cannot do for more details 查看哪些Applet能够做什么,不能做更多细节

An applet cannot establish a File on the server. applet无法在服务器上建立File 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. 如果服务器接受上载,则必须提供特定功能才能启用它,并且applet必须使用该功能。

As long as the functionality to accept an upload is on the same server, the applet can remain sand-boxed. 只要接受上载的功能在同一台服务器上,applet就可以保持沙盒状态。

你可以通过(windows)任务栏看到applet的控制台,你右键单击java图标(当applet运行时它应该出现在右下方)并右键单击>打开控制台,你可以调试那里,这是堆栈的位置小程序的痕迹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM