上传高达50MB的视频到服务器 [英] uploading video upto 50MB to the server

查看:153
本文介绍了上传高达50MB的视频到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Andr​​oid应用程序将视频上​​传到server.When IAM试图上传的视频我的应用程序显示
04-23 13:07:41.602:ERROR / dalvikvm堆(519) 超出内存在15607356字节分配我不知道为什么错误occuring.My问题是:  我们可以上传大小的视频高达50MB使用下面的code?如果有人知道请帮我...

 私人无效doFileUpload(){
            HttpURLConnection的康恩= NULL;
            DataOutputStream类DOS = NULL;
            inStream中的DataInputStream = NULL;
            字符串lineEnd =\ r \ N的;
            串twoHyphens = - ;
            字符串边界=*****;
            INT读取动作,方bytesAvailable,缓冲区大小;
            byte []的缓冲区;
            INT maxBufferSize = 8 * 1024 * 1024;
            光标C =(MainscreenActivity.JEEMAHWDroidDB).query((MainscreenActivity.TABLE_Name),新的String [] {
                     (MainscreenActivity.COL_HwdXml)},NULL,NULL,NULL,NULL,
                              空值);
             如果(c.getCount()!= 0){
             c.moveToLast();
             的for(int i = c.getCount() -  1; I> = 0;我 - ){
                  值= c.getString(0);
             }
             }
            字符串urlString =价值+/ upload_file.php;
            尝试
            {
             // ------------------客户端请求
                UUID唯一键= UUID.randomUUID();
                FNAME = uniqueKey.toString();
                Log.e(唯一的名称,FNAME);

            的FileInputStream的FileInputStream =新的FileInputStream(新文件(selectedPath));
            网址URL =新的URL(urlString);
            康恩=(HttpURLConnection类)url.openConnection();
            conn.setDoInput(真正的);
            conn.setDoOutput(真正的);
            conn.setUseCaches(假);
            conn.setRequestMethod(POST);
            conn.setRequestProperty(连接,保持活动);
            conn.setRequestProperty(内容类型,多部分/格式数据;边界=+界);
            DOS =新DataOutputStream类(conn.getOutputStream());
             dos.writeBytes(twoHyphens +边界+ lineEnd);
             dos.writeBytes(。内容处置表格数据;名称= \UploadedFile的\文件名= \+ FNAME + +分机++ lineEnd);
             dos.writeBytes(lineEnd);
             方bytesAvailable = fileInputStream.available();
             的System.out.println(字节:--------->+方bytesAvailable);
             BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
             的System.out.println(缓冲区大小:--------->+ BUFFERSIZE);
             缓冲区=新的字节[BUFFERSIZE]
             的System.out.println(缓冲:--------->+缓冲);
             读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
             的System.out.println(读取的字节:--------->+读取动作);
             而(读取动作大于0)
             {
              dos.write(缓冲液,0,BUFFERSIZE);
              方bytesAvailable = fileInputStream.available();
              BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
              读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
              的System.out.println(飞返);
             }
             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);
            }
 

解决方案

尝试更换

 方bytesAvailable = fileInputStream.available();
 的System.out.println(字节:--------->+方bytesAvailable);
 BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
 的System.out.println(缓冲区大小:--------->+ BUFFERSIZE);
 缓冲区=新的字节[BUFFERSIZE]
 的System.out.println(缓冲:--------->+缓冲);
 读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
 的System.out.println(读取的字节:--------->+读取动作);
 而(读取动作大于0)
 {
  dos.write(缓冲液,0,BUFFERSIZE);
  方bytesAvailable = fileInputStream.available();
  BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
  读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
  的System.out.println(飞返);
 }
 

 缓冲区=新的字节[8192];
读取动作= 0;
而((读取动作= fileInputStream.read(缓冲液))!=  -  1){
    dos.write(BUF,0,读取动作);
}
 

解释:的 阅读() 会回报你的字节是读或 1 如果最终数量流的到达,并没有进一步的数据可以被读出。这也就罢了,缓存与它读取数据的第N个字节。 !本(A = B)= C 语法首先分配B到A的话,值与C(此处执行,分配结果读取动作,比较 1 )。因此,循环运行,直到每个字节从开始读取到流的末尾

在每个缓冲区中的数据通过写<一href="http://docs.oracle.com/javase/6/docs/api/java/io/OutputStream.html#write%28byte%5b%5d,%20int,%20int%29"相对=nofollow> 。因为我们知道,从读取动作多少字节缓存实际上是新的阅读,我们告诉字节来只写在 0 字节为读取动作。不需要检查 InputStream.available()(甚至可能返回如果不知道流的长度没有意义的结果)或任何其他方法。

注:更改,要而(读取动作&GT; 0)引入了一个微妙的差异。它将停止阅读,如果你读0字节,但是并没有达到流的末尾。这情况下是合法的,虽然它是pretty的安全的假设,它不会发生。如果你使用,你是安全的读取动作&GT; = 0 虽然

I want upload videos from my android apps to server.When iam trying to upload the video my apps shows
04-23 13:07:41.602: ERROR/dalvikvm-heap(519): Out of memory on a 15607356-byte allocation.I dont know why the error is occuring.My question is Can we upload video of size upto 50MB using the below code?if anybody knows please help me...

private void doFileUpload(){
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 8*1024*1024;
            Cursor c = (MainscreenActivity.JEEMAHWDroidDB).query((MainscreenActivity.TABLE_Name), new String[] {
                     (MainscreenActivity.COL_HwdXml)}, null, null, null, null,
                              null);
             if(c.getCount()!=0){
             c.moveToLast();
             for(int i=c.getCount()-1; i>=0; i--) {
                  value=c.getString(0);           
             }
             }
            String urlString = value+"/upload_file.php";
            try
            {
             //------------------ CLIENT REQUEST
                UUID uniqueKey = UUID.randomUUID();
                fname = uniqueKey.toString();
                Log.e("UNIQUE NAME",fname);

            FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            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=\"" + fname + "."+extension+"" + lineEnd);
             dos.writeBytes(lineEnd);
             bytesAvailable = fileInputStream.available();
             System.out.println("BYTES:--------->"+bytesAvailable);
             bufferSize = Math.min(bytesAvailable, maxBufferSize);
             System.out.println("BUFFER SIZE:--------->"+bufferSize);
             buffer = new byte[bufferSize];
             System.out.println("BUFFER:--------->"+buffer);
             bytesRead = fileInputStream.read(buffer,0,bufferSize);
             System.out.println("BYTES READ:--------->"+bytesRead);
             while (bytesRead > 0)
             {
              dos.write(buffer, 0, bufferSize);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              System.out.println("RETURNED");
             }
             dos.writeBytes(lineEnd);
             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

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

解决方案

try replacing

 bytesAvailable = fileInputStream.available();
 System.out.println("BYTES:--------->"+bytesAvailable);
 bufferSize = Math.min(bytesAvailable, maxBufferSize);
 System.out.println("BUFFER SIZE:--------->"+bufferSize);
 buffer = new byte[bufferSize];
 System.out.println("BUFFER:--------->"+buffer);
 bytesRead = fileInputStream.read(buffer,0,bufferSize);
 System.out.println("BYTES READ:--------->"+bytesRead);
 while (bytesRead > 0)
 {
  dos.write(buffer, 0, bufferSize);
  bytesAvailable = fileInputStream.available();
  bufferSize = Math.min(bytesAvailable, maxBufferSize);
  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  System.out.println("RETURNED");
 }

with

buffer = new byte[8192];
bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
    dos.write(buf, 0, bytesRead);
}

Explained: read() will return you the number of bytes it read or -1 if the end of the stream is reached and no further data can be read. It also fills the first N bytes of buffer with the data it read. The (a = b) != c syntax first assigns b to a, then compares the value to c (here: execute read, assign result to bytesRead, compare to -1). Therefore the loop runs until every byte is read from start to the end of the stream.

After each read the data in buffer is written via write. Since we know from bytesRead how much bytes in buffer are actually newly read bytes we tell write to write only the bytes from 0 to bytesRead. No need to check InputStream.available() (which may even return meaningless results if the length of the stream is not known) or any other method.

Note: changing that to while (bytesRead > 0) introduces a subtle difference. It will stop reading if you read 0 bytes but did not reach the end of the stream. That case is legal although it is pretty safe to assume that it does not happen. You are safer if you use bytesRead >= 0 though.

这篇关于上传高达50MB的视频到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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