嗨,我有一个 Java Applet,它可以播放本地文件系统的文件。当我将 Applet 嵌入网页时,出现 io.FilePermission 异常。我知道解决这个问题的唯一方法是让我的 Applet 签名,但我不希望这样做。
我如何从它自己的 jar 文件中读取该文件?据我所知,这将是最好的方法。
感谢大家的帮助。
这是我的代码
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 文件读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988516/