2012-09-02 89 views
0

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

我有和.3gp音频格式,它没有mather格式,我只是想要在我想要的服务器目录中的文件。任何指南,教程,评论,请。我将不胜感激。

回答

3

我写这段代码发送一个文件到服务器:

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

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"; 
    } 
} 
+0

你知道我在哪里可以学习如何做到这一点? –

+0

我已将此代码添加到我的eclipse中,但它不识别MultipartEntity,HttpClient等。并且也没有导入建议。因此,可以建议我如何解决此问题。 –

0

您可以使用HttpClientPOST你的音乐文件,一些服务器应用程序。

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

为了转码音频数据,您可以使用FFmpeg等。

相关问题