0

我创建了小程序来从剪贴板捕获图像。当我在 HTML 中运行小程序时,它会抛出accesscontrolexception access denied (java.awt.awtpermission accessclipboard)

PasteImageApplet.Java

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.io.ByteArrayOutputStream;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
import javax.swing.JLabel;

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);
            }catch (Exception e){
                JOptionPane.showMessageDialog(null, "No image found.");
                return "";
            }

            BufferedImage img = (BufferedImage) object;

            BufferedImage bimg = null;
            int w = img.getWidth(null);
            int h = img.getHeight(null);
            bimg = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);


            ImageIcon ii = new ImageIcon(img);
            ImageObserver is = ii.getImageObserver();

            bimg.getGraphics().setColor(new Color(255, 255, 255));
            bimg.getGraphics().fillRect(0, 0, w, h);
            bimg.getGraphics().drawImage(ii.getImage(), 0, 0, is);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(stream);
            jpeg.encode(bimg);                                               

            URL u = new URL(server);
            URLConnection con = u.openConnection();
            //String boundary = "-----------------------------7d637a1aa100de";
            con.setDoOutput(true);
            con.getOutputStream().write(stream.toByteArray());
            /*con.getOutputStream().write(((String)
                    "--"+boundary+"\r\n "+
                    "Content-Disposition: form-data; name=\"img\"; filename=\"filename\"\r\n"+
                    "Content-Type: image/jpeg\r\n "+
                    "Content-Transfer-Encoding: base64\r\n\r\n" +
                    Base64.encodeBytes(stream.toByteArray())).getBytes());*/
            con.connect();
            InputStream inputStream = con.getInputStream();
            byte [] urlBytes = new byte [inputStream.available()];
            inputStream.read(urlBytes);
            url = new String(urlBytes);
            System.out.print(url);
            lbl.setText("image pasted");
        } catch (Exception exc){
            lbl.setText("an error occurred: " + exc.getMessage());
            /*if (ShowExceptions.ShowExceptions)
                exc.printStackTrace();*/
        }
        return url;
    }

    public void init() {
        lbl = new JLabel("");
        lbl.setText("applet started");
        add(lbl);
        toolkit = Toolkit.getDefaultToolkit();
        clipboard = toolkit.getSystemClipboard();
    }
}

索引.html

<html>
<title>The Hello, World Applet</title>
<hr>
<applet code="PasteImageApplet.class" width="320" height="120">
</hr>
</html>

图片 在此处输入图像描述

4

2 回答 2

1

签署jar并将访问的主要代码放入AccessController的clipbord中,如下所示

AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
        // perform the security-sensitive operation here
        return null;
    }
});
于 2013-07-19T09:16:43.047 回答
0

未签名的小程序无法访问系统剪贴板,因此请使用javax.jnlp.ClipboardService并创建 jar/jnlp 文件。

PasteImageApplet.java

try {
    ClipboardService cs =
    (ClipboardService)ServiceManager.lookup("javax.jnlp.ClipboardService");
    img = (Image)cs.getContents().getTransferData(DataFlavor.imageFlavor);
} catch (Exception ex) {
}

PasteImage.jnlp

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.6+" href="PasteImage.jnlp">
  <information>
  <title>Paste Image</title>
  <vendor>ABC</vendor>
</information>
<resources>
  <java version="1.6+"
      href="http://java.sun.com/products/autodl/j2se" />
  <jar href="PasteImage.jar" main="true"/>
</resources>
<applet-desc name="PasteImage"
             main-class="PasteImageApplet"
             width="320" height="120">
</applet-desc>

索引.html

<applet width="320" height="120">
<param name="jnlp_href" value="PasteImage.jnlp" />
</applet>
于 2013-07-22T02:33:43.183 回答