【问题标题】:how can i download audio file from server by url如何通过 url 从服务器下载音频文件
【发布时间】:2026-02-04 13:15:01
【问题描述】:

如何通过 url 从服务器下载音频文件并将其保存到 sdcard。

我正在使用下面的代码:

public void uploadPithyFromServer(String imageURL, String fileName) {

    try {
        URL url = new URL(GlobalConfig.AppUrl + imageURL);
        File file = new File(fileName);

        Log.d("ImageManager", "download begining");
        Log.d("ImageManager", "download url:" + url);
        Log.d("ImageManager", "downloaded file name:" + fileName);
        /* Open a connection to that URL. */
        URLConnection con = url.openConnection();

        InputStream is = con.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is, 1024 * 50);
        FileOutputStream fos = new FileOutputStream("/sdcard/" + file);
        byte[] buffer = new byte[1024 * 50];

        int current = 0;
        while ((current = bis.read(buffer)) != -1) {
            fos.write(buffer, 0, current);
        }

        fos.flush();
        fos.close();
        bis.close();

    } catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);
    }

}

上面的代码没有下载音频文件。 如果在清单文件中使用任何权限,请告诉我..(我使用了互联网权限) 请帮忙

谢谢..

【问题讨论】:

  • 你怎么知道音频文件没有被下载?

标签: android download


【解决方案1】:

你还必须添加

android.permission.WRITE_EXTERNAL_STORAGE

如果您想将数据写入 sd 卡,请授予权限。

如果您遇到任何 IOExceptions,请同时发布您的 logcat 输出。

【讨论】:

    【解决方案2】:

    您的示例没有指定请求方法和一些 mimetypes 和东西。
    在这里你会找到一个 mimetypes 列表http://www.webmaster-toolkit.com/mime-types.shtml
    找到与您相关的 mimetypes,并将其添加到代码中下面指定的 mimetypes。

    哦,顺便说一句,下面是普通的 Java 代码。您必须替换将文件存储在 sdcard 上的位。目前没有模拟器或手机来测试该部分 另请参阅此处的 sd 存储权限文档:http://developer.android.com/reference/android/Manifest.permission_group.html#STORAGE

      public static void downloadFile(String hostUrl, String filename)
      {
        try {      
        File file = new File(filename);
        URL server = new URL(hostUrl + file.getName());
    
    
        HttpURLConnection connection = (HttpURLConnection)server.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.addRequestProperty("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*");
        connection.addRequestProperty("Accept-Language", "en-us,zh-cn;q=0.5");
        connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
    
        connection.connect();
        InputStream is = connection.getInputStream();
        OutputStream os = new FileOutputStream("c:/temp/" + file.getName());
    
        byte[] buffer = new byte[1024];
        int byteReaded = is.read(buffer);
        while(byteReaded != -1)
        {
          os.write(buffer,0,byteReaded);
          byteReaded = is.read(buffer);
        }
    
       os.close();
    
      } catch (IOException e) {
        e.printStackTrace();
      }
    

    然后调用,

     downloadFile("http://localhost/images/bullets/", "bullet_green.gif" );
    

    编辑: 糟糕的编码器我。 将该输入 InputStream 包装在 BufferedInputStream 中。无需指定缓冲区大小等。
    默认值很好。

    【讨论】: