您好我有一个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();
}
}
}
答案 0 :(得分:2)
尝试
URL url = getDocumentBase();
AudioClip audioClip = getAudioClip(url, "music/JButton.wav")
项目结构