如何发送音频文件以存储在服务器中 [英] How can I send an audio file to be store in a server

查看:97
本文介绍了如何发送音频文件以存储在服务器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to know how can I convert and audio file store in the SD Card of an android device, to send the audio to the server via internet. I have look a lot for it, but everything is different.

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

I have and .3gp audio format, and it does not matther the format, I just want to get the file in the server directory I want it to be. Any guide, tutorial, comments, please. I would greatly appreciate it.

推荐答案

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

I wrote this piece of code to send a file to server:

path =您要发送的文件的绝对路径.其余的参数似乎可以自我解释.

path = the absolute path to file you want to send. remaining params seems self explanatory.

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

这篇关于如何发送音频文件以存储在服务器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆