你好,我有一个Java 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();
}
}
}发布于 2014-04-10 20:55:57
试一试
URL url = getDocumentBase();
AudioClip audioClip = getAudioClip(url, "music/JButton.wav")项目结构

https://stackoverflow.com/questions/22988516
复制相似问题