Android实现TCP断点上传,后台C#服务实现接收

终端实现大文件上传一直都是比较难的技术,其中涉及到后端与前端的交互,稳定性和流量大小,而且实现原理每个人都有自己的想法,后端主流用的比较多的是Http来实现,因为大多实现过断点下载。但稳定性不能保证,一旦断开,无法续传。所以得采用另一种流行的做法,TCP上传大文件。

网上查找了一些资料,大多数是断点下载,然后就是单独的C#端的上传接收,或是HTTP的,或是只有android端的,由于任务紧所以之前找的首选方案当然是Http先来实现文件上传,终端采用Post方法,将文件直接传至后端,后端通过File来获得。

android端:

RequestParams params = new RequestParams();
 File file = getTempFile();//获得本地文件
        try {
            params.put("file", file);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        AsyncHttpUtil.post(URL + "/UpLoad", params, new JsonHttpResponseHandler() {
……

后端:

var file = Request.Files["file"];
  file.SaveAs(upFileName);

还有其它更好的处理方法,也可以传流进来,不通过file文件格式。 在网络好的情况下没什么问题,但网络差点后来经常上传一半掉线或多个客户端上传出现连不上的情况,对于大文件极不稳定,所以赶紧研发TCP协议文件断点上传。

也有网友实现了Http断点上传,既然大文件不行,那就将文件分割成小文件来上传,纯NET的主要方法:

上传:

 bool result = true;
            long cruuent = 0;

            FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader bReader = new BinaryReader(fStream); 
            
            //模拟断点上传,第一次只上传 100 个字节
            long length = 100;

            fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);

            #region 开始上传文件
            try
            {

                
                byte[] data; 
                #region 分割文件上传
                for (; cruuent <= length; cruuent = cruuent + byteCount)
                {
                    if (cruuent + byteCount > length)
                    {
                        data = new byte[Convert.ToInt64((length - cruuent))];
                        bReader.Read(data, 0, Convert.ToInt32((length - cruuent)));
                    }
                    else
                    {
                        data = new byte[byteCount];
                        bReader.Read(data, 0, byteCount);
                    }
                    try
                    {
                        Hashtable parms = new Hashtable();
                        parms.Add("fileName", fileName);
                        parms.Add("npos", cruuent.ToString());

                        byte[] byRemoteInfo = PostData(serverPath + "UpLoadServer.aspx", data, parms);

                    }
                    catch (Exception ex)
                    {
                        msg = ex.ToString();
                        result = false;
                        break;
                    }
                #endregion
                }
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                result = false;
            }
            finally
            {
                bReader.Close();
                fStream.Close();

            }

            GC.Collect();

先将文件分割成小流,npos为断点的位置,即已经上传了的大小,然后循环上传所有包。

后台接收:

 /// <summary>
    /// 保存文件(从URL参数中获取文件名、当前指针,将文件流保存到当前指针后)
    /// 如果是第一次上传,则当前指针为0,代码执行与续传一样,只不过指针没有偏移
    /// </summary>
    public void SaveUpLoadFile()
    {

        string fileName = Request.Params["fileName"];
        long npos = Convert.ToInt64(Request.Params["npos"]);

        int upLoadLength = Convert.ToInt32(Request.InputStream.Length);
       
        string path = Server.MapPath("/UpLoadServer");
        fileName = path + "//UpLoad//" + fileName;

        FileStream fStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        //偏移指针
        fStream.Seek(npos, SeekOrigin.Begin);

        //从客户端的请求中获取文件流
        BinaryReader bReader = new BinaryReader(Request.InputStream);
        try
        {
            byte[] data = new byte[upLoadLength];
            bReader.Read(data, 0, upLoadLength);
            fStream.Write(data, 0, upLoadLength);
        }
        catch
        {
            //TODO 添加异常处理
        }
        finally
        {
            //释放流
            fStream.Close();
            bReader.Close();
        }
    }

 重点在 fStream.Seek(npos, SeekOrigin.Begin); 从断点位置接收保存。

 有兴趣的可以自己实现。

 现在主要讲讲客户端TCP上传,后台TCP接收,主要思路为:android端读取本地文件将文件名,文件大小上传至服务器(文件名必须是全局唯一),服务器将根据文件名查询是否上传过,若是上传过,将已传文件的大小即断点位置传给终端,终端接收后先保存断点位置,然后从断点位置读取文件断续上传,直到全部完成。若没上传过则服务器创建缓存文件接收。

看看代码Android:

String head = "Length=" + uploadFile.length() + ";filename=" + filename

 Socket socket = new Socket("192.168.0.123", 7080);
                    OutputStream outStream = socket.getOutputStream();
                    outStream.write(head.getBytes());//发送

                    PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
                    String response = StreamTool.readLine(inStream);//读取
                    String[] items = response.split(";");

 final String position = items[0].substring(items[0].indexOf("=") + 1);//断点位置
                        final String serviceurl = items[1].substring(items[1].indexOf("=") + 1);//保存到服务器路径

RandomAccessFile fileOutStream = new RandomAccessFile(uploadFile, "r");
                        fileOutStream.seek(Integer.valueOf(position));//从断点位置开始读取文件
                        byte[] buffer = new byte[1024];
                        int len = -1;
                        int length = Integer.valueOf(position);//已经上传的大小,用于本地显示
                        while ( (len = fileOutStream.read(buffer)) != -1) {
                            outStream.write(buffer, 0, len);
                            length += len;
                            Message msg = new Message();
                            msg.getData().putInt("size", length);
                            // 更新上传的进度                           handler.sendMessage(msg);

                        }
if (length == uploadFile.length()) {
//如果相等,则说明上传成功
}
fileOutStream.close(); outStream.close(); inStream.close(); socket.close(); 

 后端处理:

 private static TcpListener listener;//服务器监听
   IPAddress ipHost = IPAddress.Any;
  listener = new TcpListener(ipHost, 7080);
                    listener.Start();//开启监听

 Socket remoteSocketClient = listener.AcceptSocket();
                    device = new Device(remoteSocketClient);
//开启一个线程去处理
                    threaddev = new Thread(new ThreadStart(device.Scan));
                    device.curentThread = threaddev;
                    threaddev.IsBackground = true;
                    threaddev.Start();
Scan处理方法:
 string[] items = strGetContent.Split(';');
                    string filelength = items[0].Substring(items[0].IndexOf("=") + 1);
                    string filename = items[1].Substring(items[1].IndexOf("=") + 1);
 //文件保存完整路径
                            filePath = Path.Combine(directoryPath, filename);
//断点位置
                        long position = 0;
                        if (File.Exists(filePath))
                        {
                            using (FileStream reader = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
                            {
                                position = reader.Length;
                            }
                        }
//返回消息
 response = "position=" + position + ";serviceurl=" +  dirPath + "/" + filename) ;
                       

                        //服务器收到客户端的请求信息后,给客户端返回响应信息:;position=0  
                        //serviceurl 服务生保存的文件位置   /PlayFiles/video/2016/07/04/1141142221.mp4
                        bufferSend = Encoding.UTF8.GetBytes(response);
                        remoteSocketClient.Send(bufferSend);

然后处理续传内容:

 //获得文件内容
                        byte[] buffer = new byte[BufferSize];
                        int received = 0;
                        long receive, length = long.Parse(filelength);
                        FileInfo file = new FileInfo(filePath);
                        using (FileStream writer = file.Open(file.Exists ? FileMode.Append : FileMode.CreateNew, FileAccess.Write, FileShare.None))
                        {
                            receive = writer.Length;
                            while (receive < length)
                            {
                                if ((received = remoteSocketClient.Receive(buffer)) == 0)
                                {
                                    Program.MessageAdd(" IP【" + remoteSocketClient.RemoteEndPoint.ToString() + "】接收暂停!");
                                    break;
                                }
                                writer.Write(buffer, 0, received);
                                writer.Flush();
                                receive += (long)received;
                            }

                        }

if (receive == length)
                        {
                            Program.MessageAdd(" IP【" + remoteSocketClient.RemoteEndPoint.ToString() + "】接收" + filename + "完成!");
}

主要原理还是从断点位置上传和接收。

这里只是讲了最主要的代码功能,还有很多细节处理,比如终端要显示进度,所以还要保存进度,后端文件的保存会不会错位,还有多文件上传会不会乱,多客户端上传是创建新线程还是有线程池来处理等等 。

posted @ 2016-08-05 09:07  欢醉  阅读(3025)  评论(3编辑  收藏  举报