[英]Applet - Unable to write file
我正在嘗試從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>
我也沒有收到任何錯誤。 請指教。
這是因為applet存在於自己的沙盒中,在那里它們需要特殊權限才能執行某些操作,例如讀取或寫入客戶端計算機的磁盤。 請記住,applet在客戶端計算機的上下文中執行,它們是guest虛擬機,需要遵循house規則
查看哪些Applet能夠做什么,不能做更多細節
applet無法在服務器上建立File
。 這不是服務器的工作方式。 如果服務器接受上載,則必須提供特定功能才能啟用它,並且applet必須使用該功能。
只要接受上載的功能在同一台服務器上,applet就可以保持沙盒狀態。
你可以通過(windows)任務欄看到applet的控制台,你右鍵單擊java圖標(當applet運行時它應該出現在右下方)並右鍵單擊>打開控制台,你可以調試那里,這是堆棧的位置小程序的痕跡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.