FileHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using CoreFS.CA06;
  9. namespace Core.Mes.Client.Comm.Server
  10. {
  11. /// <summary>
  12. /// 远程文件上传下载管理
  13. /// </summary>
  14. public class FileHelper
  15. {
  16. private static string fileserver = "pssserver";
  17. public static OpeBase _ob;
  18. /// <summary>
  19. /// 文件上传
  20. /// </summary>
  21. /// <param name="files">List<FileBean> FileBean集合</param>
  22. /// <returns>bool true成功 false失败</returns>
  23. public static bool Upload(List<FileBean> files)
  24. {
  25. Init(null);
  26. CoreClientParam ccp = new CoreClientParam();
  27. ccp.ServerName = "com.steering.comm.file.FtpStorage";
  28. ccp.MethodName = "upload";
  29. ccp.ServerParams = new object[] { files };
  30. ccp = _ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  31. if (ccp == null)
  32. {
  33. throw new MESException("服务端处理失败!");
  34. }
  35. if (ccp.ReturnCode < 0)
  36. {
  37. throw new MESException(ccp.ReturnInfo, ccp.ReturnCode, ccp.ReturnInfo);
  38. }
  39. return true;
  40. }
  41. /// <summary>
  42. /// 文件下载
  43. /// </summary>
  44. /// <param name="pathName">远程文件存储路径或文件名(包含完整路径)</param>
  45. /// <returns>List<FileBean> FileBean集合</returns>
  46. public static List<FileBean> Download(string pathName)
  47. {
  48. Init(null);
  49. CoreClientParam ccp = new CoreClientParam();
  50. ccp.ServerName = "com.steering.comm.file.FtpStorage";
  51. ccp.MethodName = "download";
  52. ccp.ServerParams = new object[] { pathName };
  53. ccp = _ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  54. if (ccp == null)
  55. {
  56. throw new MESException("服务端处理失败!");
  57. }
  58. if (ccp.ReturnCode < 0)
  59. {
  60. throw new MESException(ccp.ReturnInfo, ccp.ReturnCode, ccp.ReturnInfo);
  61. }
  62. return Convert((ArrayList)ccp.ReturnObject);
  63. }
  64. /// <summary>
  65. /// 文件删除
  66. /// </summary>
  67. /// <param name="pathName">远程文件存储路径或文件名(包含完整路径)</param>
  68. /// <returns>bool true成功 false失败</returns>
  69. public static bool Delete(string pathName)
  70. {
  71. Init(null);
  72. CoreClientParam ccp = new CoreClientParam();
  73. ccp.ServerName = "com.steering.comm.file.FtpStorage";
  74. ccp.MethodName = "delete";
  75. ccp.ServerParams = new object[] { pathName };
  76. ccp = _ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  77. if (ccp == null)
  78. {
  79. throw new MESException("服务端处理失败!");
  80. }
  81. if (ccp.ReturnCode < 0)
  82. {
  83. throw new MESException(ccp.ReturnInfo, ccp.ReturnCode, ccp.ReturnInfo);
  84. }
  85. return true;
  86. }
  87. /// 将文件转换为二进制数组
  88. /// </summary>
  89. /// <param name="path">文件路径</param>
  90. /// <returns></returns>
  91. public static byte[] FileToArray(string path)
  92. {
  93. try
  94. {
  95. if (System.IO.File.Exists(path))
  96. {
  97. FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
  98. byte[] buffer = new byte[stream.Length];
  99. stream.Read(buffer, 0, System.Convert.ToInt32(stream.Length));
  100. stream.Flush();
  101. stream.Close();
  102. return buffer;
  103. }
  104. else
  105. {
  106. return new byte[0];
  107. }
  108. }
  109. catch (Exception exp)
  110. {
  111. return new byte[0];
  112. }
  113. }
  114. /// <summary>
  115. /// 二进制转换成图片
  116. /// </summary>
  117. /// <param name="Bytes">二进制数组</param>
  118. /// <returns></returns>
  119. public static Bitmap BytesToBitmap(byte[] Bytes)
  120. {
  121. Bitmap bitmap = null;
  122. MemoryStream ImageMem = new MemoryStream(Bytes, true);
  123. ImageMem.Write(Bytes, 0, Bytes.Length);
  124. bitmap = new Bitmap(ImageMem);
  125. ImageMem.Close();
  126. return bitmap;
  127. }
  128. private static void Init(OpeBase ob)
  129. {
  130. if (_ob == null)
  131. {
  132. if (ob == null)
  133. {
  134. if (CoreCommon.coreCommon.UrlList.ContainsKey(fileserver))
  135. {
  136. _ob = new OpeBase();
  137. _ob.MainUrl = CoreCommon.coreCommon.UrlList[fileserver].Url;
  138. }
  139. else
  140. {
  141. throw new Exception("请配置好文件服务!");
  142. }
  143. }
  144. else
  145. {
  146. _ob = ob;
  147. }
  148. }
  149. }
  150. private static List<FileBean> Convert(ArrayList list)
  151. {
  152. List<FileBean> result = null;
  153. if (list != null)
  154. {
  155. result = new List<FileBean>();
  156. foreach (Hashtable ht in list)
  157. {
  158. FileBean bean = HashtalbeToBean(ht);
  159. result.Add(bean);
  160. }
  161. }
  162. return result;
  163. }
  164. private static FileBean HashtalbeToBean(Hashtable ht)
  165. {
  166. FileBean bean = null;
  167. if (ht != null && ht.Count > 0)
  168. {
  169. bean = new FileBean();
  170. if (ht.ContainsKey("file"))
  171. {
  172. bean.setFile((byte[])ht["file"]);
  173. }
  174. if (ht.ContainsKey("fileName"))
  175. {
  176. bean.setFileName(ht["fileName"].ToString());
  177. }
  178. if (ht.ContainsKey("pathName"))
  179. {
  180. bean.setPathName(ht["pathName"].ToString());
  181. }
  182. }
  183. return bean;
  184. }
  185. }
  186. }