【问题标题】:How can I send an audio file to be store in a server如何发送音频文件以存储在服务器中
【发布时间】:2012-08-27 16:23:13
【问题描述】:

我想知道如何在安卓设备的 SD 卡中转换和存储音频文件,以通过互联网将音频发送到服务器。我找了很多,但一切都不一样。

我有 .3gp 音频格式,它与格式无关,我只想在我想要的服务器目录中获取文件。请提供任何指南,教程,cmets。我将不胜感激。

【问题讨论】:

    标签: android audio webserver send


    【解决方案1】:

    我编写了这段代码来向服务器发送文件:

    path = 要发送的文件的绝对路径。 剩余的参数似乎不言自明。

        public static String sendFileToServer(String path, String urlString,
            String mimeType, String id) throws Exception {
    
    
        try {
    
            String URL = "", file_name = "";
            if (!path.equals("")) {
                file_name = path.substring(path.lastIndexOf("/") + 1);
            }
    
            File directory = new File(path);
    
            byte[] data = new byte[(int) directory.length()];
            try {
                FileInputStream fileInputStream = new FileInputStream(directory);
                fileInputStream.read(data);
    
            } catch (FileNotFoundException e) {
                System.out.println("File Not Found.");
                e.printStackTrace();
            } catch (IOException e1) {
                System.out.println("Error Reading The File.");
                e1.printStackTrace();
            }
    
            URL = Constants.WEB_ROOT_URL + urlString;
            Log.v("AppEngine Manager", "Image Upload URL: " + URL);
    
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(URL);
            ByteArrayBody bab = new ByteArrayBody(data, file_name);
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
    
            reqEntity.addPart("media_type", new StringBody(mimeType));
            reqEntity.addPart("file_name", new StringBody(file_name));
            reqEntity.addPart("file_id", new StringBody(file_name));
            reqEntity.addPart("file", bab);
    
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
    
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
    
    
            return s + "";
    
        } catch (Exception e) {
            // handle exception here
            Log.e(e.getClass().getName(), e.getMessage());
            return "exception";
        }
    }
    

    【讨论】:

    • 我已将此代码添加到我的 Eclipse 中,但它无法识别 MultipartEntity、HttpClient 等。也没有导入建议。所以你能建议我如何解决这个问题。
    【解决方案2】:

    您可以将HttpClientPOST 的音乐文件用于某些服务器应用程序。

    例如,在 PHP 中,您将能够通过$_FILE['key'] 读取文件。实际存储和处理取决于您的具体要求。此外,您没有指定是否希望服务器将转码后的文件输出回来。

    例如,您可以使用FFmpeg 对音频数据进行转码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      相关资源
      最近更新 更多