java.io.FilePermission 异常 - 从 jar 文件中读取? [英] java.io.FilePermission exception - Read from jar file?

查看:37
本文介绍了java.io.FilePermission 异常 - 从 jar 文件中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java Applet,它可以播放我本地文件系统的文件.当我在网页中嵌入 Applet 时,出现 io.FilePermission 异常.我知道解决这个问题的唯一方法是让我的 Applet 签名,但我不想这样做.

Hi I have a Java Applet which plays a file of my local file system. When I embed the Applet in a web page I get a io.FilePermission Exception. I understand that the only way round this is to get my Applet signed which I don't wish to.

我将如何从它自己的 jar 文件中读取文件?据我所知,这将是最好的方法.

How would I go about read the file from it's own jar file?, As I understand this will be the best way.

感谢大家的帮助.

这是我的代码

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class PlaySoundsApplet extends Applet implements ActionListener{
  Button play,stop;
  AudioClip audioClip;

  public void init(){
  play = new Button("  Play in Loop  ");
  add(play);
  play.addActionListener(this);
  stop = new Button("  Stop  ");
  add(stop);
  stop.addActionListener(this);
  audioClip = getAudioClip(getCodeBase(), "clip.wav"); //Exception here trying to read from the www root instead of jar file

  }

  public void actionPerformed(ActionEvent ae){
  Button source = (Button)ae.getSource();
  if (source.getLabel() == "  Play in Loop  "){
  audioClip.play();
  }
  else if(source.getLabel() == "  Stop  "){
  audioClip.stop();
  }
  }
}

更新代码:

  public class PlaySoundsApplet extends Applet implements ActionListener{
  Button play,stop;
  AudioClip audioClip;

  public void init(){
  play = new Button("  Play in Loop  ");
  add(play);
  play.addActionListener(this);
  stop = new Button("  Stop  ");
  add(stop);
  stop.addActionListener(this);
  try {
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("resources/clip.wav"));
    Clip audioClip = AudioSystem.getClip();
    audioClip.open(audioInputStream);
} catch (UnsupportedAudioFileException e1) {System.out.println("Unsoported Error");} 
  catch (IOException e1) {System.out.println("IO Error");} catch (LineUnavailableException e) {System.out.println("Line unavailable Error");}
  }

  public void actionPerformed(ActionEvent ae){
  Button source = (Button)ae.getSource();
  if (source.getLabel() == "  Play in Loop  "){
  audioClip.play();
  }
  else if(source.getLabel() == "  Stop  "){
  audioClip.stop();
  }
  }
}

推荐答案

尝试

URL url = getDocumentBase();

AudioClip audioClip = getAudioClip(url, "music/JButton.wav")

项目结构

这篇关于java.io.FilePermission 异常 - 从 jar 文件中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆