我正在从远程服务器下载音频文件,然后打算在应用程序上实际播放之前将其保存到存储中。 我正在使用sliderbridge类来显示进度,但是当我尝试将其保存到存储时,我在下载完成时收到此错误:
java.io.FileNotFoundException
相关代码:
public void actionPerformed(ActionEvent ae) {
final String file=_cat+".mp3";
if(FileSystemStorage.getInstance().exists(file))
{FileSystemStorage.getInstance().delete(file);};
String path="/"+file;
ConnectionRequest r = new ConnectionRequest() {
protected void postResponse() {
try{
String path1=FileSystemStorage.getInstance().getAppHomePath();
String path2=path1+"/"+file;
InputStream stream =FileSystemStorage.getInstance().openInputStream(path2);
OutputStream out = Storage.getInstance().createOutputStream(file);
Util.copy(stream, out);
Util.cleanup(stream);
Util.cleanup(out);
}
catch(Exception ex)
{ex.printStackTrace();}
}
protected void handleException(Exception err) {
Dialog.show("Network Error!!", "Are you connected to the internet? Check your connection...And Click To Start Again", "Ok", null);
overall.removeComponent(Welcome);
overall.removeComponent(sb);
overall.invalidate();
overall.revalidate();
}
};
r.setUrl("http://abcde.com/images/"+file);
r.setPost(false);
try
{
sb=new SliderBridge(r);
NetworkManager.getInstance().addToQueue(r);
overall.addComponent(Welcome);
overall.addComponent(sb);
overall.repaint();
overall.invalidate();
overall.revalidate();
}
catch (Exception ex) {
ex.printStackTrace();
}
答案 0 :(得分:0)
我开始工作......我用过这个
ConnectionRequest r;
r = new ConnectionRequest() {
int chr;
int size=1024;
byte[] data = new byte[size];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] final_dat;
@Override
protected void readResponse(InputStream input) throws IOException {
while ((chr = input.read(data,0,size)) != -1){
buffer.write(data,0, chr);
}
final_dat=buffer.toByteArray();
}
protected void postResponse() {
try{
isp=new ByteArrayInputStream(final_dat);
OutputStream out = Storage.getInstance().createOutputStream(file);
}
Util.copy(isp, out);
Util.cleanup(isp);
Util.cleanup(out);
}
catch(Exception ex)
{ex.printStackTrace();}
}