終端實現大文件上傳一直都是比較難的技術,其中涉及到後端與前端的交互,穩定性和流量大小,並且實現原理每一個人都有本身的想法,後端主流用的比較多的是Http來實現,由於大多實現過斷點下載。但穩定性不能保證,一旦斷開,沒法續傳。因此得采用另外一種流行的作法,TCP上傳大文件。前端
網上查找了一些資料,大多數是斷點下載,而後就是單獨的C#端的上傳接收,或是HTTP的,或是隻有android端的,因爲任務緊因此以前找的首選方案固然是Http先來實現文件上傳,終端採用Post方法,將文件直接傳至後端,後端經過File來得到。android
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的主要方法:網絡
上傳:dom
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爲斷點的位置,即已經上傳了的大小,而後循環上傳全部包。socket
後臺接收:ide
/// <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); 從斷點位置接收保存。post
有興趣的能夠本身實現。
如今主要講講客戶端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 + "完成!"); }
主要原理仍是從斷點位置上傳和接收。
這裏只是講了最主要的代碼功能,還有不少細節處理,好比終端要顯示進度,因此還要保存進度,後端文件的保存會不會錯位,還有多文件上傳會不會亂,多客戶端上傳是建立新線程仍是有線程池來處理等等 。