跟着教程学习MP3播放器编写,遇到奇怪事,扩展名为MP3、jpg、gif的都可以下载,唯独lrc的下载总失败,实在找不到原因了,求助!
跟着教程学习MP3播放器编写,实现了从自己搭建的服务器下载MP3文件,并播放的功能,但是遇到一个问题,我的下载程序的代码在运行时,下载a01.mp3、a01.jpg、a01.gif、a01.txt等文件都可以正常下载,唯独下载a01.lrc文件时出现异常,下载失败。但如果把a01.lrc文件名改为a01.txt,就可以下载成功,实在找不出问题原因了,请大家帮忙,必结贴!
代码如下:
负责下载的线程的代码:mp3Name的值赋为01.mp3、a01.jpg、a01.gif、a01.txt等文件都可以正常下载,唯独下载a01.lrc文件时出现异常。
- Java code
class DownloadThread implements Runnable { private Mp3Info mp3Info = null; public DownloadThread(Mp3Info mp3Info) { this.mp3Info = mp3Info; } @Override public void run() { // 下载地址http://192.168.1.102/testWeb/歌曲名 // 根据MP3文件的名字,生成下载地址 //String mp3Name = mp3Info.getMp3Name(); //String mp3Name = "a04.lrc"; String mp3Name=""; try { mp3Name = URLEncoder.encode("a04.lrc","UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } String mp3Url = mp3url_head + URLEncoder.encode(mp3Name); // //生成下载文件所用的对象 // HttpDownloader httpDownloader = new HttpDownloader(); // 将文件下载下来,并存储到SDCard中 int result = httpDownloader.downFile(mp3Url, mp3Path, URLEncoder.encode(mp3Name)); String resultMessage = null; if (result == -1) { resultMessage = mp3Name + "下载失败"; cancelled = true; } else if (result == 1) { resultMessage = mp3Name + "已存在,无需重复下载"; cancelled = true; } else if (result == 0) { resultMessage = mp3Name + "下载成功"; } // 发送特定action的广播 Intent Broadcastintent = new Intent(); Broadcastintent.setAction("android.intent.action.MY_RECEIVER"); Broadcastintent.putExtra("resultMessage", resultMessage); sendBroadcast(Broadcastintent); } }httpDownloader.downFile代码
- Java code
/** * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在 */ public int downFile(String urlStr, String path, String fileName) { InputStream inputStream = null; try { if (fileUtils.isFileExist(fileName, path)) { return 1; } else { inputStream = getInputStreamFromUrl(urlStr); File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream); if (resultFile == null) { return -1; } } } catch (Exception e) { e.printStackTrace(); return -1; } finally { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return 0; }getInputStreamFromUrl代码
- Java code
public InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException { url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setRequestMethod("GET"); InputStream inputStream = urlConn.getInputStream(); this.lenghtOfFile = urlConn.getContentLength(); return inputStream; }fileUtils.write2SDFromInput代码
- Java code
/** * 将一个InputStream里面的数据写入到SD卡中 */ public File write2SDFromInput(String path, String fileName, InputStream input) { File file = null; OutputStream output = null; try { creatSDDir(path); file = createFileInSDCard(fileName, path); output = new FileOutputStream(file); byte buffer[] = new byte[4 * 1024]; int temp; while ((temp = input.read(buffer)) != -1) { this.total += temp;// total = total + temp output.write(buffer, 0, temp); } output.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { output.close(); } catch (Exception e) { e.printStackTrace(); } } return file; }
异常情况:
08-01 09:24:37.934: W/System.err(7773): java.io.FileNotFoundException: http://192.168.1.100/testWeb/a04.lrc
08-01 09:24:37.934: W/System.err(7773): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
08-01 09:24:37.944: W/System.err(7773): at hk.download.HttpDownloader.getInputStreamFromUrl(HttpDownloader.java:108)
08-01 09:24:37.944: W/System.err(7773): at hk.download.HttpDownloader.downFile(HttpDownloader.java:75)
08-01 09:24:37.954: W/System.err(7773): at hk.mp3player.service.DownloadService$DownloadThread.run(DownloadService.java:175)
08-01 09:24:37.954: W/System.err(7773): at java.lang.Thread.run(Thread.java:1096)
08-01 09:24:37.954: W/System.err(7773): java.lang.NullPointerException
08-01 09:24:37.954: W/System.err(7773): at hk.download.HttpDownloader.downFile(HttpDownloader.java:87)
08-01 09:24:37.954: W/System.err(7773): at hk.mp3player.service.DownloadService$DownloadThread.run(DownloadService.java:175)
08-01 09:24:37.964: W/System.err(7773): at java.lang.Thread.run(Thread.java:1096)
08-01 09:24:38.004: W/System.err(7773): java.io.FileNotFoundException: http://192.168.1.100/testWeb/a03.lrc
08-01 09:24:38.004: W/System.err(7773): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
08-01 09:24:38.014: W/System.err(7773): at hk.download.HttpDownloader.getInputStreamFromUrl(HttpDownloader.java:108)
08-01 09:24:38.014: W/System.err(7773): at hk.download.HttpDownloader.downFile(HttpDownloader.java:75)
08-01 09:24:38.014: W/System.err(7773): at hk.mp3player.service.DownloadService$DownloadlrcThread.run(DownloadService.java:215)
08-01 09:24:38.014: W/System.err(7773): at java.lang.Thread.run(Thread.java:1096)
08-01 09:24:38.014: W/System.err(7773): java.lang.NullPointerException
08-01 09:24:38.064: W/System.err(7773): at hk.download.HttpDownloader.downFile(HttpDownloader.java:87)
08-01 09:24:38.064: W/System.err(7773): at hk.mp3player.service.DownloadService$DownloadlrcThread.run(DownloadService.java:215)
08-01 09:24:38.064: W/System.err(7773): at java.lang.Thread.run(Thread.java:1096)
请大家帮帮忙!看看到底哪里出了问题,谢谢!!!
[解决办法]
FileNotFoundException 文件没找到
是不是跟服务器设置有关?
[解决办法]
解决办法,在IIS中设置.lrc的MIME,操作办法图文说明:
http://blog.csdn.net/aminfo/article/details/7820520
[解决办法]
这是MARS老师的源码,很不错的,一是你的服务器有没有.LRC文件,想必这个原因的可能性不大,其次是服务器的原因,楼上的方法试试的,真心的不懂
[解决办法]
我之前也有 APK文件找不到 是服务器上没有这种文件的类型 要自己添加