跟着教程学习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)

解决方法!!

Win7 在IIS中增加MIME类型文件,步骤如下:

第一步:开始 ->  所有程序 -> 管理工具 -> Internet 信息服务(IIS)管理器

第二步:点击“MIME”类型:

第三步:在上图右侧有一个“添加”按钮,如下图:

第四步:填写MIME设置,例如添加.lrc文件,如下图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你可以参考以下 PHP 代码实现图片文件上传,其中使用了 `$_FILES` 和 `pathinfo()` 函数来获取上传文件的信息,使用了 `in_array()` 函数来判断上传文件的扩展名是否合法,最后使用了 `move_uploaded_file()` 函数将上传的文件保存到指定目录。 ```php <?php if(isset($_POST['submit'])){ $allowed_extensions = array("jpeg", "png", "gif"); $file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); $file_size = $_FILES['file']['size']; $file_tmp = $_FILES['file']['tmp_name']; if(in_array($file_extension, $allowed_extensions) === false){ $errors[] = "扩展名不合法,请上传 JPEG、PNG 或 GIF 格式的图片!"; } if($file_size > 2097152){ $errors[] = "文件大小不能超过 2MB!"; } if(empty($errors) == true){ move_uploaded_file($file_tmp, "uploads/" . $_FILES['file']['name']); echo "文件上传成功!"; }else{ print_r($errors); } } ?> <html> <head> <title>图片上传</title> </head> <body> <form method="POST" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" name="submit" value="上传" /> </form> </body> </html> ``` 在这段代码中,`$allowed_extensions` 数组存储了允许上传的图片扩展名,`$file_extension` 变量使用 `pathinfo()` 函数获取上传文件的扩展名,`$file_size` 变量获取上传文件的大小,`$file_tmp` 变量获取上传文件的临时存储路径。 然后使用 `in_array()` 函数判断 `$file_extension` 是否在 `$allowed_extensions` 数组中,如果不在,则说明上传的文件不是被允许的图片格式,抛出一个错误信息。 接下来判断上传文件的大小是否超过 2MB,如果超过则同样抛出一个错误信息。 最后使用 `move_uploaded_file()` 函数将上传的文件从临时目录移动到指定的目录中,同时输出上传成功的信息。如果出现了错误,使用 `print_r()` 函数输出错误信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值