哪个buffersize应该在将音频上传到服务器时使用?

时间:2014-12-27 05:58:29

标签: android audio

我正在处理从android到java的音频上传。当我上传任何小于5MB的文件然后我可以在服务器上传但超过5 MB的文件我无法上传。 这是我的代码......

         DataOutputStream dos = null;  
         String lineEnd = "\r\n";
         String twoHyphens = "--";
         String boundary = "*****";
         int bytesRead, bytesAvailable, bufferSize;
         byte[] buffer;
         int maxBufferSize = 20 * 1024 * 1024; 
         File sourceFile = new File(sourceFileUri); 

         if (!sourceFile.isFile()) 
         {
               dialog.dismiss(); 
               Log.e("uploadFile", "Source File not exist :" + selectedAudioPath);

               runOnUiThread(new Runnable() 
               {
                   public void run() 
                   {
                    messageText.setText("Source File not exist : " +selectedAudioPath);
                   }
               }); 

               return 0;
         }
         else
         {
               try 
               { 
                   File f  = new File(selectedAudioPath);
                   audio = f.getName();
                   System.out.println("Audio Name : " + audio);

                   upLoadServerUri = "MY URL";

                   FileInputStream fileInputStream = new FileInputStream(sourceFile);
                   URL url = new URL(upLoadServerUri);
                   System.out.println("URL : " + url);
                   conn = (HttpURLConnection) url.openConnection(); 
                   conn.setDoInput(true);
                   conn.setDoOutput(true);
                   conn.setUseCaches(false);
                   conn.setRequestMethod("POST");
                   conn.setRequestProperty("Connection", "Keep-Alive");
                   conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                   conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                   conn.setRequestProperty("audiofile", audio);

                   dos  = new DataOutputStream(conn.getOutputStream());

                   dos.writeBytes(twoHyphens + boundary + lineEnd); 
                   dos.writeBytes("Content-Disposition: form-data; name=\"audiofile\";filename=\"" + audio + "\"" + lineEnd);
                   dos.writeBytes(lineEnd);

                   bytesAvailable = fileInputStream.available(); 

                   bufferSize = Math.min(bytesAvailable, maxBufferSize);
                   buffer = new byte[bufferSize];

                   System.out.println("MaxBuffersize : " + maxBufferSize);
                   System.out.println("BytesAvailable : " + bytesAvailable);
                   System.out.println("Buffersize : " + bufferSize);
                   System.out.println("Buffer : " + buffer);

                   bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

                   System.out.println("BytesRead : " + bytesRead);

                   while (bytesRead > 0) 
                   {
                       dos.write(buffer, 0, bufferSize);
                       bytesAvailable = fileInputStream.available();
                       bufferSize = Math.min(bytesAvailable, maxBufferSize);
                       bytesRead = fileInputStream.read(buffer, 0, bufferSize);   
                    }

这是我的bufferize的logcat输出: Maxbuffersize:20971520 bytesavailable:8414449
buffersize:8414449 缓冲区:[B @ 413a6528 bytesread:8414449

&安培;错误是:16829082字节分配时内存不足。

现在请告诉我应该指定哪个尺寸???

0 个答案:

没有答案