将分块文件从android上传到服务器
我正在尝试从android向asp.net服务器发送分块文件。我的客户端代码如下所示:将分块文件从android上传到服务器,android,asp.net,upload,httpclient,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(end
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);
}
公共异步任务上载(文件){
final int cSize=1024*1024;//块的大小
final long pieces=file.length()/cSize//用于返回文件长度。
HttpPost请求=新的HttpPost(端点);
BufferedInputStream=新BufferedInputStream(新文件输入流(文件));
for(int i=0;i
服务器端的上传方法应该是什么样的?我必须启动连接才能发送每个区块,还是由MultipartEntity处理?服务器端的方法是否为每个区块上传一次又一次地调用?
我已尝试查找资源,但找不到任何演示如何在服务器端处理分块文件的资源。如果有人能为我提供任何指针,我将不胜感激。您可以尝试以下代码,您的视频应作为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);
您可以尝试以下代码,您的视频应作为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);
对于否决我的问题的人。至少在否决之前要努力回答这个问题。对于像我这样的初学者来说,这是非常令人沮丧的,因为他们在网上找不到任何有用的资源。非常抱歉,你在上传图像或视频吗?@user3151766我正在向此人上传视频谁否决了我的问题。至少在否决之前要努力回答这个问题。对于像我这样的初学者来说,这是非常令人沮丧的,因为他们在网上找不到任何有用的资源。非常抱歉,你在上传图像或视频吗?@user3151766我在上传视频。在这种情况下,不会有ny块。它将是base64格式的字符串。在这种情况下,将不会有任何块。它将是base64格式的字符串。