从android下载chunked文件到服务器

时间:2016-05-28 03:12:09

标签: android asp.net upload httpclient

我试图将分块文件从android发送到asp.net服务器。我的客户端代码如下所示:

Public async Task<String> Upload(File file){
final int cSize = 1024 * 1024; // size of chunk
final long pieces = file.length()/cSize // used to return file length.

HttpPost request = new HttpPost(endpoint);

BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));

for (int i= 0; i< pieces; i++) {
    byte[] buffer = new byte[cSize];

    if(stream.read(buffer) ==-1)
      break;

    MultipartEntity entity = new MultipartEntity();
    entity.addPart("chunk_id", new StringBody(String.valueOf(i))); //Chunk Id used for identification.
    request.setEntity(entity);
    ByteArrayInputStream arrayStream = new ByteArrayInputStream(buffer);

    entity.addPart("file_data", new InputStreamBody(arrayStream, filename));

    HttpClient client = app.getHttpClient();
    client.execute(request);
}

上传方法在服务器端应该是什么样的?我是否必须启动连接以发送每个块或由MultipartEntity处理?服务器端的方法也是为每个块上传一次又一次地调用? 我试图寻找资源,但无法找到任何可以证明如何在服务器端处理分块文件的资源。如果有人能为我提供任何指示,我将非常感激。

1 个答案:

答案 0 :(得分:0)

您可以尝试下面的代码,您的视频应该作为base64字符串发送:

byte[] imageBytes = Convert.FromBase64String("Base64MessageString");

string FileName = "FileName";

MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

string FilePath = System.Web.Hosting.HostingEnvironment.MapPath("~\\Video\\") + subPath + "\\";
string FullFilePath = FilePath + FileName;

FileInfo imageFile = new FileInfo(FullFilePath);

FileStream fs = new FileStream(FullFilePath, FileMode.Create);
ms.Write(imageBytes, 0, imageBytes.Length);
ms.WriteTo(fs);