public class FileHelper { #region /*向客户端发送文件数据*/ /// <summary> /// 向HTTP客户端传送文件 /// </summary> /// <param name="ctx">HttpListenerContext对像</param> /// <param name="filePath">文件路径</param> /// <param name="fileName">文件名称</param> public void TransferToHttpClientFile(HttpListenerContext ctx, string filePath, string fileName) { FileStream fs = File.OpenRead(filePath); //待下载的文件 long startPos = 0; string range = ctx.Request.Headers["Range"]; bool isResume = string.IsNullOrEmpty(range); if (!isResume) //断点续传请求 { //格式bytes=9216- startPos = long.Parse(range.Split('=')[1].Split('-')[0]); ctx.Response.StatusCode = 206; ctx.Response.ContentLength64 = fs.Length - startPos; fs.Position = startPos; //设置传送的起始位置 } else { ctx.Response.ContentLength64 = fs.Length; } Console.WriteLine("request header"); Console.WriteLine(ctx.Request.Headers.ToString()); //设置返回内容的MIME类型 //ctx.Response.ContentType = "application/octet-stream"; //ctx.Response.AddHeader("Content-Disposition", "attachment;FileName=" + fileName); using (Stream output = ctx.Response.OutputStream) { try { Console.WriteLine("response header"); Console.WriteLine(ctx.Response.Headers.ToString()); SendStream(fs, output); //文件传输 output.Close(); } catch (HttpListenerException e) //在未写完所有文件时,如果客户端关闭连接,会抛此异常 { Console.WriteLine(e.Message); } } fs.Close(); } /// <summary> /// 发送数据流 /// </summary> /// <param name="fs">文件流</param> /// <param name="outStream">OutputStream对象</param> public void SendStream(Stream fs, Stream outStream) { byte[] buffer = new byte[1024]; int read = 0; while ((read = fs.Read(buffer, 0, 1024)) > 0) { outStream.Write(buffer, 0, read); //System.Threading.Thread.Sleep(1000); //模拟慢速设备 } if (fs != null) { fs.Close(); } if (outStream != null) { outStream.Close(); } } #endregion #region /*读取TXT文件*/ /// <summary> /// 读取TXT文件 /// </summary> /// <param name="filePath">TXT文件路径</param> /// <returns>List</returns> public static List<string> ReadLineTxt(string filePath) { return new List<string>(File.ReadAllLines(filePath,Encoding.Default)); } #endregion #region /*字符串空格处理*/ /// <summary> /// 向右添加空格 /// </summary> /// <param name="str">string</param> /// <param name="len">int最大长度</param> /// <returns>string</returns> public static string AddRSpace(string str, int len) { int tmplen = len - GetLength(str); tmplen = tmplen >= 0 ? tmplen : 0; for (int i = 0; i < tmplen; i++) { str = str + " "; } return str; } /// <summary> /// 向左添加空格 /// </summary> /// <param name="str">string</param> /// <param name="len">int最大长度</param> /// <returns>string</returns> public static string AddLSpace(string str, int len) { int tmplen = len - GetLength(str); tmplen = tmplen >= 0 ? tmplen : 0; for (int i = 0; i < tmplen; i++) { str = " " + str; } return str; } /// <summary> /// 取字符串长度 /// 一个汉字算二个字符 /// </summary> /// <param name="str">string字符串</param> /// <returns>int</returns> public static int GetLength(string str) { if (str.Length == 0) return 0; ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; byte[] s = ascii.GetBytes(str); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) { tempLen += 2; } else { tempLen += 1; } } return tempLen; } /// <summary> /// substring处理中文,让汉字二个字符的处理方法 /// </summary> /// <param name="a_StartIndex"></param> /// <param name="a_Cnt"></param> /// <returns></returns> public static string SubStringExt(string s, int a_StartIndex, int a_Cnt) { byte[] l_byte = System.Text.Encoding.Default.GetBytes(s); return System.Text.Encoding.Default.GetString(l_byte, a_StartIndex, a_Cnt).Trim(); } #endregion }