如何上传从SD卡音频服务器的android [英] How to Upload audio from sd-card to server in android

查看:266
本文介绍了如何上传从SD卡音频服务器的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想音频文件上传从SD卡的特定文件夹服务器。但是当我尝试这样做,我收到下面的错误。

错误:到/ mnt / SD卡/ AudioRecorder:打开失败:EISDIR(是一个目录)

和我的全是logcat中如下。

 十二月1日至23日:44:18.920:E /调试(6164):错误:到/ mnt / SD卡/ AudioRecorder:打开失败:EISDIR(是一个目录)
12月1日至23日:44:18.920:E /调试(6164):java.io.FileNotFoundException:到/ mnt / SD卡/ AudioRecorder:打开失败:EISDIR(是一个目录)
12月1日至23日:44:18.920:E /调试(6164):在libcore.io.IoBridge.open(IoBridge.java:448)
12月1日至23日:44:18.920:E /调试(6164):在java.io.FileInputStream中的<&初始化GT;(FileInputStream.java:78)
12月1日至23日:44:18.920:E /调试(6164):在iqualtech.skirr.Record_AudioPG.doAudioFileUpload(Record_AudioPG.java:295)
12月1日至23日:44:18.920:E /调试(6164):在iqualtech.skirr.Record_AudioPG.sto precording(Record_AudioPG.java:271)
12月1日至23日:44:18.920:E /调试(6164):在iqualtech.skirr.Record_AudioPG.access $ 0(Record_AudioPG.java:265)
12月1日至23日:44:18.920:E /调试(6164):在iqualtech.skirr.Record_AudioPG $ 4.onClick(Record_AudioPG.java:124)
12月1日至23日:44:18.920:E /调试(6164):在android.view.View.performClick(View.java:3517)
12月1日至23日:44:18.920:E /调试(6164):在android.view.View $ PerformClick.run(View.java:14155)
12月1日至23日:44:18.920:E /调试(6164):在android.os.Handler.handleCallback(Handler.java:605)
12月1日至23日:44:18.920:E /调试(6164):在android.os.Handler.dispatchMessage(Handler.java:92)
12月1日至23日:44:18.920:E /调试(6164):在android.os.Looper.loop(Looper.java:154)
12月1日至23日:44:18.920:E /调试(6164):在android.app.ActivityThread.main(ActivityThread.java:4624)
12月1日至23日:44:18.920:E /调试(6164):在java.lang.reflect.Method.invokeNative(本机方法)
12月1日至23日:44:18.920:E /调试(6164):在java.lang.reflect.Method.invoke(Method.java:511)
12月1日至23日:44:18.920:E /调试(6164):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:809)
12月1日至23日:44:18.920:E /调试(6164):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12月1日至23日:44:18.920:E /调试(6164):在dalvik.system.NativeStart.main(本机方法)
12月1日至23日:44:18.920:E /调试(6164):libcore.io.ErrnoException:产生的原因打开失败:EISDIR(是一个目录)
12月1日至23日:44:18.920:E /调试(6164):在libcore.io.IoBridge.open(IoBridge.java:437)
12月1日至23日:44:18.920:E /调试(6164):16 ...更多

和低于此多一个logcat的是

 十二月1日至23日:44:18.926:E / AndroidRuntime(6164):致命异常:主要
12月1日至23日:44:18.926:E / AndroidRuntime(6164):显示java.lang.NullPointerException
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在iqualtech.skirr.Record_AudioPG.doAudioFileUpload(Record_AudioPG.java:344)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在iqualtech.skirr.Record_AudioPG.sto precording(Record_AudioPG.java:271)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在iqualtech.skirr.Record_AudioPG.access $ 0(Record_AudioPG.java:265)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在iqualtech.skirr.Record_AudioPG $ 4.onClick(Record_AudioPG.java:124)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在android.view.View.performClick(View.java:3517)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在android.view.View $ PerformClick.run(View.java:14155)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在android.os.Handler.handleCallback(Handler.java:605)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在android.os.Handler.dispatchMessage(Handler.java:92)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在android.os.Looper.loop(Looper.java:154)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在android.app.ActivityThread.main(ActivityThread.java:4624)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在java.lang.reflect.Method.invokeNative(本机方法)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在java.lang.reflect.Method.invoke(Method.java:511)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:809)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12月1日至23日:44:18.926:E / AndroidRuntime(6164):在dalvik.system.NativeStart.main(本机方法)

我的code

 私人无效doAudioFileUpload(){
        HttpURLConnection的康恩= NULL;
        DataOutputStream类DOS = NULL;
        DataInputStream以inStream中= NULL;
        字符串existingFileName = Environment.getExternalStorageDirectory()
                .getPath()+/ AudioRecorder /;        的System.out.println(内部doupload第二路径是===
                + existingFileName);        字符串lineEnd =\\ r \\ n;
        串twoHyphens = - ;
        字符串边界=*****;
        INT读取动作,方bytesAvailable,缓冲区大小;
        字节[]缓冲区;
        INT MAXBUFFERSIZE = 1 * 1024 * 1024 * 1024;
        字符串urlString =HTTP://链接服务器/文件夹名称/ upload_audio.php
        尝试{
            // ------------------客户端请求
            的FileInputStream的FileInputStream =新的FileInputStream(新文件(
                    existingFileName));
            //打开一个URL连接到这个Servlet
            网址URL =新的URL(urlString);
            //打开HTTP连接的URL
            康恩=(HttpURLConnection类)url.openConnection();
            //允许输入
            conn.setDoInput(真);
            //允许输出
            conn.setDoOutput(真);
            //不要使用缓存副本。
            conn.setUseCaches(假);
            //使用POST方法。
            conn.setRequestMethod(POST);
            conn.setRequestProperty(连接,保持活动);
            conn.setRequestProperty(内容类型,
                    的multipart / form-data的;边界=+边界);
            DOS =新的DataOutputStream类(conn.getOutputStream());
            dos.writeBytes(twoHyphens +边界+ lineEnd);
            dos.writeBytes(内容处置:表格数据;名称= \\UploadedFile的\\;文件名= \\
                    + existingFileName +\\+ lineEnd);
            dos.writeBytes(lineEnd);
            //创建最大大小的缓冲区
            参考bytesAvailable = fileInputStream.available();
            BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
            缓冲区=新的字节[缓冲区大小]
            //读取文件,并将其写入形式...
            读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);
            而(读取动作大于0){
                dos.write(缓冲液,0,缓冲区大小);
                参考bytesAvailable = fileInputStream.available();
                BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
                读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);
            }
            //发送文件数据后necesssary多部分的表单数据...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd);
            //关闭流
            Log.e(调试,文件写入);
            fileInputStream.close();
            dos.flush();
            dos.close();
        }赶上(MalformedURLException的前){
            Log.e(调试,错误:+ ex.getMessage(),除息);
        }赶上(IOException异常IOE){
            Log.e(调试,错误:+ ioe.getMessage(),IOE);
        }
        // ------------------读取服务器响应
        尝试{
            inStream中=新DataInputStream所(conn.getInputStream());
            字符串str;            而((海峡= inStream.readLine())!= NULL){
                Log.e(调试,服务器响应+ STR);
            }
            inStream.close();        }赶上(IOException异常ioex){
            Log.e(调试,错误:+ ioex.getMessage(),ioex);
        }
    }


解决方案

日志信息是明确的,你想上传一个目录,而不是一个文件:

 字符串existingFileName = Environment.getExternalStorageDirectory()
                .getPath()+/ AudioRecorder /

/ AudioRecorder /不是音频file.You在年底或许应该附加的音频文件名。

如果你的所有文件上传的目录,后来想想这样做的(意向)服务中。


  1. 的文件名作为参数添加到您的方法:

  2. 在你的活动,循环到文件列表,并为每个文件:

     为(字符串的文件名:filesL​​ist){                意向意图=新意图(这一点,UploadIntentService.class);
                    捆绑PARAMS =新包();
                    params.putString(UploadIntentService.KEY_FILE_NAME,文件名);
                    activity.startService(意向);
    }


  3. 该服务:

     公共类UploadIntentService扩展IntentService {公共静态最后弦乐KEY_FILE_NAME =文件名;
    公共UploadIntentService(){
        超级(UploadIntentService);
                }@覆盖
    保护无效onHandleIntent(意向意图){
    字符串文件名= intent.getBundle()getStringExtra(UploadIntentService.KEY_FILE_NAME)。doAudioFileUpload(文件名);
           }   私人无效doAudioFileUpload(字符串文件名){
        HttpURLConnection的康恩= NULL;
        DataOutputStream类DOS = NULL;
        DataInputStream以inStream中= NULL;
        字符串existingFileName = Environment.getExternalStorageDirectory()
                .getPath()+/ AudioRecorder /+文件名;    的System.out.println(内部doupload第二路径是===
                + existingFileName);    字符串lineEnd =\\ r \\ n;
        串twoHyphens = - ;
        字符串边界=*****;
        INT读取动作,方bytesAvailable,缓冲区大小;
        字节[]缓冲区;
        INT MAXBUFFERSIZE = 1 * 1024 * 1024 * 1024;
        字符串urlString =HTTP://链接服务器/文件夹名称/ upload_audio.php
        尝试{
            // ------------------客户端请求
            的FileInputStream的FileInputStream =新的FileInputStream(新文件(
                    existingFileName));
            //打开一个URL连接到这个Servlet
            网址URL =新的URL(urlString);
            //打开HTTP连接的URL
            康恩=(HttpURLConnection类)url.openConnection();
            //允许输入
            conn.setDoInput(真);
            //允许输出
            conn.setDoOutput(真);
            //不要使用缓存副本。
            conn.setUseCaches(假);
            //使用POST方法。
            conn.setRequestMethod(POST);
            conn.setRequestProperty(连接,保持活动);
            conn.setRequestProperty(内容类型,
                    的multipart / form-data的;边界=+边界);
            DOS =新的DataOutputStream类(conn.getOutputStream());
            dos.writeBytes(twoHyphens +边界+ lineEnd);
            dos.writeBytes(内容处置:表格数据;名称= \\UploadedFile的\\;文件名= \\
                    + existingFileName +\\+ lineEnd);
            dos.writeBytes(lineEnd);
            //创建最大大小的缓冲区
            参考bytesAvailable = fileInputStream.available();
            BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
            缓冲区=新的字节[缓冲区大小]
            //读取文件,并将其写入形式...
            读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);
            而(读取动作大于0){
                dos.write(缓冲液,0,缓冲区大小);
                参考bytesAvailable = fileInputStream.available();
                BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
                读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);
            }
            //发送文件数据后necesssary多部分的表单数据...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd);
            //关闭流
            Log.e(调试,文件写入);
            fileInputStream.close();
            dos.flush();
            dos.close();
        }赶上(MalformedURLException的前){
            Log.e(调试,错误:+ ex.getMessage(),除息);
        }赶上(IOException异常IOE){
            Log.e(调试,错误:+ ioe.getMessage(),IOE);
        }
        // ------------------读取服务器响应
        尝试{
            inStream中=新DataInputStream所(conn.getInputStream());
            字符串str;        而((海峡= inStream.readLine())!= NULL){
                Log.e(调试,服务器响应+ STR);
            }
            inStream.close();    }赶上(IOException异常ioex){
            Log.e(调试,错误:+ ioex.getMessage(),ioex);
        }
    }

    希望它能帮助


PS:没有测试code

I am trying to upload audio file from particular folder of SD-Card to server. but when i try to do that i am getting following error.

error: /mnt/sdcard/AudioRecorder: open failed: EISDIR (Is a directory)

and my full logcat is as below.

01-23 12:44:18.920: E/Debug(6164): error: /mnt/sdcard/AudioRecorder: open failed: EISDIR (Is a directory)
01-23 12:44:18.920: E/Debug(6164): java.io.FileNotFoundException: /mnt/sdcard/AudioRecorder: open failed: EISDIR (Is a directory)
01-23 12:44:18.920: E/Debug(6164):  at libcore.io.IoBridge.open(IoBridge.java:448)
01-23 12:44:18.920: E/Debug(6164):  at java.io.FileInputStream.<init>(FileInputStream.java:78)
01-23 12:44:18.920: E/Debug(6164):  at iqualtech.skirr.Record_AudioPG.doAudioFileUpload(Record_AudioPG.java:295)
01-23 12:44:18.920: E/Debug(6164):  at iqualtech.skirr.Record_AudioPG.stopRecording(Record_AudioPG.java:271)
01-23 12:44:18.920: E/Debug(6164):  at iqualtech.skirr.Record_AudioPG.access$0(Record_AudioPG.java:265)
01-23 12:44:18.920: E/Debug(6164):  at iqualtech.skirr.Record_AudioPG$4.onClick(Record_AudioPG.java:124)
01-23 12:44:18.920: E/Debug(6164):  at android.view.View.performClick(View.java:3517)
01-23 12:44:18.920: E/Debug(6164):  at android.view.View$PerformClick.run(View.java:14155)
01-23 12:44:18.920: E/Debug(6164):  at android.os.Handler.handleCallback(Handler.java:605)
01-23 12:44:18.920: E/Debug(6164):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 12:44:18.920: E/Debug(6164):  at android.os.Looper.loop(Looper.java:154)
01-23 12:44:18.920: E/Debug(6164):  at android.app.ActivityThread.main(ActivityThread.java:4624)
01-23 12:44:18.920: E/Debug(6164):  at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:44:18.920: E/Debug(6164):  at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:44:18.920: E/Debug(6164):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
01-23 12:44:18.920: E/Debug(6164):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
01-23 12:44:18.920: E/Debug(6164):  at dalvik.system.NativeStart.main(Native Method)
01-23 12:44:18.920: E/Debug(6164): Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
01-23 12:44:18.920: E/Debug(6164):  at libcore.io.IoBridge.open(IoBridge.java:437)
01-23 12:44:18.920: E/Debug(6164):  ... 16 more

and one more logcat below this is

01-23 12:44:18.926: E/AndroidRuntime(6164): FATAL EXCEPTION: main
01-23 12:44:18.926: E/AndroidRuntime(6164): java.lang.NullPointerException
01-23 12:44:18.926: E/AndroidRuntime(6164):     at iqualtech.skirr.Record_AudioPG.doAudioFileUpload(Record_AudioPG.java:344)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at iqualtech.skirr.Record_AudioPG.stopRecording(Record_AudioPG.java:271)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at iqualtech.skirr.Record_AudioPG.access$0(Record_AudioPG.java:265)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at iqualtech.skirr.Record_AudioPG$4.onClick(Record_AudioPG.java:124)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at android.view.View.performClick(View.java:3517)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at android.view.View$PerformClick.run(View.java:14155)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at android.os.Handler.handleCallback(Handler.java:605)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at android.os.Looper.loop(Looper.java:154)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at android.app.ActivityThread.main(ActivityThread.java:4624)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at java.lang.reflect.Method.invoke(Method.java:511)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
01-23 12:44:18.926: E/AndroidRuntime(6164):     at dalvik.system.NativeStart.main(Native Method)

My Code

private void doAudioFileUpload() {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;
        String existingFileName = Environment.getExternalStorageDirectory()
                .getPath() + "/AudioRecorder/";

        System.out.println("Inside of doupload nd path is === "
                + existingFileName);

        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024 * 1024;
        String urlString = "http://link to server/folder-name/upload_audio.php";
        try {
            // ------------------ CLIENT REQUEST
            FileInputStream fileInputStream = new FileInputStream(new File(
                    existingFileName));
            // open a URL connection to the Servlet
            URL url = new URL(urlString);
            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + existingFileName + "\"" + lineEnd);
            dos.writeBytes(lineEnd);
            // create a buffer of maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // close streams
            Log.e("Debug", "File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (MalformedURLException ex) {
            Log.e("Debug", "error: " + ex.getMessage(), ex);
        } catch (IOException ioe) {
            Log.e("Debug", "error: " + ioe.getMessage(), ioe);
        }
        // ------------------ read the SERVER RESPONSE
        try {
            inStream = new DataInputStream(conn.getInputStream());
            String str;

            while ((str = inStream.readLine()) != null) {
                Log.e("Debug", "Server Response " + str);
            }
            inStream.close();

        } catch (IOException ioex) {
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        }
    }    

解决方案

The log message is clear, you are trying to upload a directory, not a file :

String existingFileName = Environment.getExternalStorageDirectory()
                .getPath() + "/AudioRecorder/"

/AudioRecorder/ is not an audio file.You should perhaps append the audio filename at the end.

If you have to upload all the files in the dir, then think about doing that inside an (Intent)service.

  1. Add the filename as parameter to your method :
  2. In your activity, Loop into the files list, and for each file :

    for(String filename : filesList ){
    
                    Intent intent = new Intent(this, UploadIntentService.class);
                    Bundle params = new Bundle();
                    params.putString(UploadIntentService.KEY_FILE_NAME, filename);
                    activity.startService(intent);
    }
    

  3. The Service :

    public class UploadIntentService extends IntentService {
    
    public static final String KEY_FILE_NAME= "filename";
    public UploadIntentService() {
        super("UploadIntentService");
                }
    
    @Override
    protected void onHandleIntent(Intent intent) { 
    String filename = intent.getBundle().getStringExtra(UploadIntentService.KEY_FILE_NAME);      
    
    doAudioFileUpload(filename);
           }
    
       private void doAudioFileUpload(String filename) {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;
        String existingFileName = Environment.getExternalStorageDirectory()
                .getPath() + "/AudioRecorder/"+filename;
    
        System.out.println("Inside of doupload nd path is === "
                + existingFileName);
    
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024 * 1024;
        String urlString = "http://link to server/folder-name/upload_audio.php";
        try {
            // ------------------ CLIENT REQUEST
            FileInputStream fileInputStream = new FileInputStream(new File(
                    existingFileName));
            // open a URL connection to the Servlet
            URL url = new URL(urlString);
            // Open a HTTP connection to the URL
            conn = (HttpURLConnection) url.openConnection();
            // Allow Inputs
            conn.setDoInput(true);
            // Allow Outputs
            conn.setDoOutput(true);
            // Don't use a cached copy.
            conn.setUseCaches(false);
            // Use a post method.
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + existingFileName + "\"" + lineEnd);
            dos.writeBytes(lineEnd);
            // create a buffer of maximum size
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // close streams
            Log.e("Debug", "File is written");
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (MalformedURLException ex) {
            Log.e("Debug", "error: " + ex.getMessage(), ex);
        } catch (IOException ioe) {
            Log.e("Debug", "error: " + ioe.getMessage(), ioe);
        }
        // ------------------ read the SERVER RESPONSE
        try {
            inStream = new DataInputStream(conn.getInputStream());
            String str;
    
            while ((str = inStream.readLine()) != null) {
                Log.e("Debug", "Server Response " + str);
            }
            inStream.close();
    
        } catch (IOException ioex) {
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        }
    }   
    

    Hope it helps

PS: Not tested code.

这篇关于如何上传从SD卡音频服务器的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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