在Android中的Stream(byte [])上的服务器上传VIDEO?

时间:2014-03-04 13:58:21

标签: android upload bytearray

在应用程序中,我正在捕获视频并以字节数组转换视频。字节数组上传到服务器上。

Server上字节数组的长度与我使用Web服务从设备发送的Array的长度不匹配。

解码时服务器端的视频已损坏且无法播放。

我正在使用以下代码在服务器上以流方式发送视频。

使用POST调用在Android End:

public String callRestfullWebService(String url, ArrayList<NameValuePair> listParamsWithValues, byte[] parameter,
            int serviceFormat, boolean isPostRequest) throws Exception {
        String responseString = "";
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams,
                TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT_MILISEC);
        HttpClient client = new DefaultHttpClient(httpParams);
        HttpPost postRequest = null;
        HttpGet getRequest = null;
        if(isPostRequest){
            postRequest = new HttpPost(url);
        }else{
            getRequest = new HttpGet(url);
        }
        switch (serviceFormat) {
        case GENERAL_RESTFUL_CALLING:
            break;
        case URL_ENCODED_FORM_ENTITY:
            postRequest.setEntity(new UrlEncodedFormEntity(listParamsWithValues));
            break;
        case BYTE_ENTITY_FORM:
            ByteArrayEntity byteArrayEntity = new ByteArrayEntity(parameter);
            postRequest.setEntity(byteArrayEntity);
            break;
            /*case STRING_ENTITY_FORM:
            StringEntity stringEntity=new StringEntity(parameter);
            postRequest.setEntity(stringEntity);
            break;*/
        }
        HttpResponse response = null;;
        if(isPostRequest){
            response = client.execute(postRequest);
        }else{
            response = client.execute(getRequest);
        }
        responseString = request(response); // here you get the response
        System.out.println(responseString); // this line will print the
        return responseString;
    }

在服务器端使用.NET服务:

 [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "VideoTest", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public Stream VideoTest(Stream fileContents)
    {
    try
    {
    // int length = 1024 * 4096;
    //int bytesRead = 0;
    Byte[] buffer = StreamToByteArray(fileContents);
    var newArray = new byte[buffer.Length-1];

    string path = HttpContext.Current.Server.MapPath("~/Weddingimage/" + Guid.NewGuid().ToString() + ".mp4");

    //fileContents.Write(newArray, 1, buffer.Length);
    // Array.Copy(buffer, 0, newArray, 1, buffer.Length);
    FileStream fileStream = new FileStream(path, FileMode.Create);
    for (int i = 1; i < buffer.Length; i++)
    {
    fileStream.WriteByte(buffer[i]);
    }

    //Byte[] buffer1 = StreamToByteArray(fileStream);
    //File.Create(path, buffer1.Length);
    //File.WriteAllBytes(path, buffer1);

    //int length = 1024 * 4096;
    //int bytesRead = 0;
    //Byte[] buffer = new Byte[length];
    //using (FileStream fileStream = new FileStream(path, FileMode.Create))
    //{
    // do
    // {
    // bytesRead = fileContents.Read(buffer, 0, length);
    // fileStream.Write(buffer, 0, bytesRead);
    // }
    // while (bytesRead > 0);
    //}

    string lines = "\n FileLength :" + Convert.ToString(buffer.Length) + "\r UploadTime :" + DateTime.Now + "\r";
    if (!File.Exists(HttpContext.Current.Server.MapPath("~/VideoLog.txt")))
    {
    System.IO.StreamWriter file = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath("~/VideoLog.txt"));
    file.WriteLine(lines);
    file.Close();
    }
    else
    {
    StreamWriter oFile = File.AppendText(HttpContext.Current.Server.MapPath("~/VideoLog.txt"));
    // System.IO.StreamWriter file = new System.IO.StreamWriter();
    oFile.WriteLine(lines);
    oFile.Close();
    }

    return new MemoryStream(Encoding.UTF8.GetBytes(ConvertMe.ToJson(ConvertTOjsonString("Video Uploaded."))));
    }
    catch (Exception ex)
    {
    CommonClass.SaveRX_Log("VideoFrame", "1", ex.Source, ex.Message, ex.StackTrace);
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
    return new MemoryStream(Encoding.UTF8.GetBytes(ConvertMe.ToJson(ConvertTOjsonString(ex.Message))));
    }
    }

请建议。

0 个答案:

没有答案