gpt4 book ai didi

android - 如何发送音频文件以存储在服务器中

转载 作者:行者123 更新时间:2023-12-03 00:19:35 25 4
gpt4 key购买 nike

我想知道如何转换和将音频文件存储在android设备的SD卡中,以通过互联网将音频发送到服务器。我对此有很多期待,但一切都不尽相同。

我有.3gp音频格式,并且它没有其他格式,我只想将文件保存在服务器目录中即可。请提供任何指南,教程,评论。我将不胜感激。

最佳答案

我编写了这段代码,将文件发送到服务器:

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

关于android - 如何发送音频文件以存储在服务器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12239252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com