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
{
///
/// 远程文件上传下载管理
///
public class FileHelper
{
private static string fileserver = "pssserver";
public static OpeBase _ob;
///
/// 文件上传
///
/// List FileBean集合
/// bool true成功 false失败
public static bool Upload(List 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;
}
///
/// 文件下载
///
/// 远程文件存储路径或文件名(包含完整路径)
/// List FileBean集合
public static List 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);
}
///
/// 文件删除
///
/// 远程文件存储路径或文件名(包含完整路径)
/// bool true成功 false失败
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;
}
/// 将文件转换为二进制数组
///
/// 文件路径
///
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];
}
}
///
/// 二进制转换成图片
///
/// 二进制数组
///
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 Convert(ArrayList list)
{
List result = null;
if (list != null)
{
result = new List();
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;
}
}
}