小程序 - 无法写入文件 [英] Applet - Unable to write file
本文介绍了小程序 - 无法写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从小程序编写示例文件,但没有成功.下面是代码.
小程序
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>