How to upload video file into webserver in android?

前端 未结 2 1207
旧巷少年郎
旧巷少年郎 2020-12-20 01:17

Hi I want to upload my video file into web server in Android. I followed this tutorial:

Uploading files to HTTP server using POST on Android.

But. I got thi

相关标签:
2条回答
  • 2020-12-20 01:49

    its just because of large size of video. Use chunk upload approach for large file/video.

    Here is sample code to post chunk data on server.

    public void upload(String filename, byte[] image) {
         // 1MB of chunk
        byte[][] divideByte = divideArray(image, ((1024 * 1024)/2));
        int length = divideByte.length;
        String token = "" + (int)System.currentTimeMillis();
        Log.i("System out", "divideByte:" + length);
        Log.i("System out", "token:" + token);
    
        for (int i = 0; i < length; i++) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    
            String link = "<your link>";
    
            nameValuePairs.add(new BasicNameValuePair("filename", filename));// filename
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(link);
            Log.i("System out", "divideByte[" + i + "] length :"
                    + divideByte[i].length);
            String img = Base64.encodeBytes(divideByte[i]);
            nameValuePairs.add(new BasicNameValuePair("file64", img));
    
            nameValuePairs.add(new BasicNameValuePair("token", new String(""
                    + token)));
            nameValuePairs.add(new BasicNameValuePair("chunksize", new String(""+
                    divideByte[i].length)));
            nameValuePairs.add(new BasicNameValuePair("chunkcount", new String(
                    "" + length)));
            nameValuePairs.add(new BasicNameValuePair("index", new String(""
                    + i)));
            try {
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity httpEntity = response.getEntity();
                if (httpEntity != null) {
                    String res = EntityUtils.toString(httpEntity).toString();
                    Log.i("System out", "response is :" + res);                 
                }
                httpEntity = null;
                response = null;
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                httppost = null;
                httpclient = null;
            }           
        }
    }
    
    public static byte[][] divideArray(byte[] source, int chucksize) {
    
        byte[][] ret = new byte[(int) Math.ceil(source.length
                / (double) chucksize)][chucksize];
    
        Log.i("System out","ret: "+ret.length);
        int start = 0;
    
        for (int i = 0; i < ret.length; i++) {
            if (start + chucksize > source.length) {
                System.arraycopy(source, start, ret[i], 0, source.length
                        - start);
            } else {
                System.arraycopy(source, start, ret[i], 0, chucksize);
            }
    
            start += chucksize;
        }
    
        return ret;
    }
    

    and on server side get the all chunks index wise and compose one video file.

    Hope help to you.

    0 讨论(0)
  • 2020-12-20 01:51

    Sending video to server from byte[] may cause outOfMemoryError so its better to post video from MultiPart. You can download jar file from this link. http://hc.apache.org/downloads.cgi . Download and add httpmime-4.2.jar to your project.

    public void uploadVideo(Context context, String videoPath) {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost postRequest = new HttpPost(context.getString(R.string.url_service_fbpost));
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                if(!videoPath.isEmpty()){
    
                    FileBody filebodyVideo = new FileBody(new File(videoPath));
                    reqEntity.addPart("uploaded", filebodyVideo);
                }
                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);
                }
    
                Log.e("Response: ", s.toString());
                return true;
    
            } catch (Exception e) {
                Log.e(e.getClass().getName(), e.getMessage());
                return false;
            }
    }
    
    0 讨论(0)
提交回复
热议问题