如何上传大的视频文件在服务器上? [英] How to Upload Large Video FIle on server?

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

问题描述

如何上传大的视频文件在服务器上?
我要到50至服务器上60MB的视频文件,但我不知道如何的可能。我不能上传到15MB的视频文件在服务器上。请有任何解决方案,然后让我知道。

公共静态字符串postRequestvideo_test(URL字符串,字节[]视频,
            字节[]的图像,列表数据){

 字符串结果=;
    Log.i(video_upload,视频+);
    尝试{
        HttpPost httpPost =新HttpPost(URL);
        // StringEntity本身;
        // SE =新StringEntity(数据,HTTP.UTF_8);
        // httpPost.setEntity(新UrlEn codedFormEntity(数据));
        httpPost.setEntity(新UrlEn codedFormEntity(数据,UTF-8));
        MultipartEntity mpEntity =新MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);        mpEntity.addPart(video_thumbnail,新ByteArrayBody(图像,
                imagename+.JPEG));
        mpEntity.addPart(视频,
                新ByteArrayBody(视频,海曼+.MP4));        mpEntity.addPart(user_key,新StringBody(user_key_test));
        mpEntity.addPart(VIDEO_NAME,新StringBody(video_name_test));
        mpEntity.addPart(VIDEO_DURATION,新StringBody(
                video_duration_test));        mpEntity.addPart(video_thumbnail_extn,新StringBody(
                video_thumbnail_extn_test));
        httpPost.setEntity(mpEntity);
        的HttpParams httpParameters =新BasicHttpParams();
        INT timeoutConnection = 90000000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        INT timeoutSocket = 90000000;
        HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);        DefaultHttpClient的HttpClient =新DefaultHttpClient(httpParameters);
        BasicHtt presponse HTT presponse =(BasicHtt presponse)的HttpClient
                .execute(httpPost);
        HttpEntity实体= HTT presponse.getEntity();
        如果(实体!= NULL){
            结果= EntityUtils.toString(实体);
            结果= result.trim();
        }
    }赶上(UnsupportedEncodingException五){
        e.printStackTrace();
    }赶上(ParseException的E){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }赶上(例外五){
        返回-333+ e.toString();
    }
    返回结果;
}


解决方案

//老回答,请查看下面的新的答案

您遇到可能是试图加载整个视频的问题(或任何为此事大文件)在内存中一次,因为应用程序有一个最大内存有限,允许后,如果你超过,它会导致应用程序崩溃,给你内存溢出异常。

你需要做的是打开的连接,并将其设置为保持活跃,然后打开的FileInputStream 阅读您的视频,并开始阅读和发送作为块(1-4 MB可能是一个很好的大小),直到所有文件的字节被发送。这样,你确保数据流发送数据,而你不超过允许的内存限制为您的应用程序。

// 新答案

请注意,上面的是一个古老的答案,现在我只使​​用 MultipartEntity (org.apache.http.entity.mime.MultipartEntityBuilder),它需要对自己照顾的任何文件大小

How to Upload Large Video FIle on server? I want up to 50 to 60mb video File on Server but I dont know how its possible. I could not Upload up to 15mb video file on server. Please have any solution Then let me know.

public static String postRequestvideo_test(String url, byte[] video, byte[] image, List data) {

    String result = "";
    Log.i("video_upload", video + "");
    try {
        HttpPost httpPost = new HttpPost(url);
        // StringEntity se;
        // se = new StringEntity(data, HTTP.UTF_8);
        // httpPost.setEntity(new UrlEncodedFormEntity(data));
        httpPost.setEntity(new UrlEncodedFormEntity(data, "UTF-8"));
        MultipartEntity mpEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        mpEntity.addPart("video_thumbnail", new ByteArrayBody(image,
                "imagename" + ".jpeg"));
        mpEntity.addPart("video",
                new ByteArrayBody(video, "hyman" + ".mp4"));

        mpEntity.addPart("user_key", new StringBody("user_key_test"));
        mpEntity.addPart("video_name", new StringBody("video_name_test"));
        mpEntity.addPart("video_duration", new StringBody(
                "video_duration_test"));

        mpEntity.addPart("video_thumbnail_extn", new StringBody(
                "video_thumbnail_extn_test"));
        httpPost.setEntity(mpEntity);
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 90000000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        int timeoutSocket = 90000000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
        BasicHttpResponse httpResponse = (BasicHttpResponse) httpClient
                .execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity);
            result = result.trim();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        return "-333" + e.toString();
    }
    return result;
}

解决方案

// Old Answer, please CHECK BELOW FOR NEW ANSWER

The problem you're having is probably trying to load the entire video (or for that matter any large file) in the memory at once, since the app has a maximum limited memory allowed, if you exceed that, it will cause the application to crash and give you Out of Memory exception.

What you need to do is open a connection and set it to "Keep-Alive", then open a FileInputStream for reading your video, and start reading and sending as chunks (1-4 MB is probably a good size) until all the file bytes are sent. That way you ensure the stream is sending the data while you're not exceeding your allowed memory limit for your app.

// New Answer

Please note that the above is an old answer, right now I only use MultipartEntity (org.apache.http.entity.mime.MultipartEntityBuilder) and it takes care of any file size on its own.

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

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