| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using CoreFS.CA06;
- namespace Core.Mes.Client.Comm.Server
- {
- /// <summary>
- /// 远程文件上传下载管理
- /// </summary>
- public class FileHelper
- {
- private static string fileserver = "pssserver";
- public static OpeBase _ob;
- /// <summary>
- /// 文件上传
- /// </summary>
- /// <param name="files">List<FileBean> FileBean集合</param>
- /// <returns>bool true成功 false失败</returns>
- public static bool Upload(List<FileBean> files)
- {
- Init(null);
- CoreClientParam ccp = new CoreClientParam();
- ccp.ServerName = "com.steering.comm.file.FtpStorage";
- ccp.MethodName = "upload";
- ccp.ServerParams = new object[] { files };
- ccp = _ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
- if (ccp == null)
- {
- throw new MESException("服务端处理失败!");
- }
- if (ccp.ReturnCode < 0)
- {
- throw new MESException(ccp.ReturnInfo, ccp.ReturnCode, ccp.ReturnInfo);
- }
- return true;
- }
- /// <summary>
- /// 文件下载
- /// </summary>
- /// <param name="pathName">远程文件存储路径或文件名(包含完整路径)</param>
- /// <returns>List<FileBean> FileBean集合</returns>
- public static List<FileBean> Download(string pathName)
- {
- Init(null);
- CoreClientParam ccp = new CoreClientParam();
- ccp.ServerName = "com.steering.comm.file.FtpStorage";
- ccp.MethodName = "download";
- ccp.ServerParams = new object[] { pathName };
- ccp = _ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
- if (ccp == null)
- {
- throw new MESException("服务端处理失败!");
- }
- if (ccp.ReturnCode < 0)
- {
- throw new MESException(ccp.ReturnInfo, ccp.ReturnCode, ccp.ReturnInfo);
- }
- return Convert((ArrayList)ccp.ReturnObject);
- }
- /// <summary>
- /// 文件删除
- /// </summary>
- /// <param name="pathName">远程文件存储路径或文件名(包含完整路径)</param>
- /// <returns>bool true成功 false失败</returns>
- public static bool Delete(string pathName)
- {
- Init(null);
- CoreClientParam ccp = new CoreClientParam();
- ccp.ServerName = "com.steering.comm.file.FtpStorage";
- ccp.MethodName = "delete";
- ccp.ServerParams = new object[] { pathName };
- ccp = _ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
- if (ccp == null)
- {
- throw new MESException("服务端处理失败!");
- }
- if (ccp.ReturnCode < 0)
- {
- throw new MESException(ccp.ReturnInfo, ccp.ReturnCode, ccp.ReturnInfo);
- }
- return true;
- }
- /// 将文件转换为二进制数组
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns></returns>
- public static byte[] FileToArray(string path)
- {
- try
- {
- if (System.IO.File.Exists(path))
- {
- FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
- byte[] buffer = new byte[stream.Length];
- stream.Read(buffer, 0, System.Convert.ToInt32(stream.Length));
- stream.Flush();
- stream.Close();
- return buffer;
- }
- else
- {
- return new byte[0];
- }
- }
- catch (Exception exp)
- {
- return new byte[0];
- }
- }
- /// <summary>
- /// 二进制转换成图片
- /// </summary>
- /// <param name="Bytes">二进制数组</param>
- /// <returns></returns>
- public static Bitmap BytesToBitmap(byte[] Bytes)
- {
- Bitmap bitmap = null;
- MemoryStream ImageMem = new MemoryStream(Bytes, true);
- ImageMem.Write(Bytes, 0, Bytes.Length);
- bitmap = new Bitmap(ImageMem);
- ImageMem.Close();
- return bitmap;
- }
- private static void Init(OpeBase ob)
- {
- if (_ob == null)
- {
- if (ob == null)
- {
- if (CoreCommon.coreCommon.UrlList.ContainsKey(fileserver))
- {
- _ob = new OpeBase();
- _ob.MainUrl = CoreCommon.coreCommon.UrlList[fileserver].Url;
- }
- else
- {
- throw new Exception("请配置好文件服务!");
- }
- }
- else
- {
- _ob = ob;
- }
- }
- }
- private static List<FileBean> Convert(ArrayList list)
- {
- List<FileBean> result = null;
- if (list != null)
- {
- result = new List<FileBean>();
- foreach (Hashtable ht in list)
- {
- FileBean bean = HashtalbeToBean(ht);
- result.Add(bean);
- }
- }
- return result;
- }
- private static FileBean HashtalbeToBean(Hashtable ht)
- {
- FileBean bean = null;
- if (ht != null && ht.Count > 0)
- {
- bean = new FileBean();
- if (ht.ContainsKey("file"))
- {
- bean.setFile((byte[])ht["file"]);
- }
- if (ht.ContainsKey("fileName"))
- {
- bean.setFileName(ht["fileName"].ToString());
- }
- if (ht.ContainsKey("pathName"))
- {
- bean.setPathName(ht["pathName"].ToString());
- }
- }
- return bean;
- }
- }
- }
|