424 lines
13 KiB
C#
Executable File
424 lines
13 KiB
C#
Executable File
using APServerClient.APServiceReference;
|
|
using ODK.WPF.Helper;
|
|
using System;
|
|
using System.IO;
|
|
using System.ServiceModel;
|
|
|
|
namespace Opsoft.NetCore.O3App.Services.CommonFunction
|
|
{
|
|
/// <summary>
|
|
/// 文件操作静态类
|
|
/// </summary>
|
|
public static class FileHelper
|
|
{
|
|
/// <summary>
|
|
/// 下载服务器文件
|
|
/// </summary>
|
|
/// <param name="upFileInfo"></param>
|
|
public static void WebDownLoadFile(FileUpDownInfo upFileInfo)
|
|
{
|
|
APClient apclient;
|
|
if (!string.IsNullOrEmpty(upFileInfo.ServerIP))
|
|
{
|
|
apclient = new APClient(upFileInfo.ServerIP);
|
|
}
|
|
else
|
|
{
|
|
apclient = new APClient();
|
|
}
|
|
|
|
long blockAmount = 512000L;
|
|
bool flag = false;
|
|
string sourceFilePath = upFileInfo.SourceFilePath;
|
|
string sourceFileName = upFileInfo.SourceFileName;
|
|
string targetFilePath = upFileInfo.TargetFilePath;
|
|
string targetFileName = upFileInfo.TargetFileName;
|
|
string text = Path.Combine(targetFilePath, targetFileName);
|
|
try
|
|
{
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(targetFilePath);
|
|
if (!directoryInfo.Exists)
|
|
{
|
|
directoryInfo.Create();
|
|
}
|
|
string fileInfo = apclient.Service.GetFileInfo(sourceFilePath, sourceFileName);
|
|
if (fileInfo.Contains("失败:") || fileInfo.Contains("Error:"))
|
|
{ }
|
|
else
|
|
{
|
|
long num = 0L;
|
|
foreach (string text2 in fileInfo.Split(new char[]
|
|
{
|
|
';'
|
|
}))
|
|
{
|
|
if (text2.Length > 0)
|
|
{
|
|
string[] array2 = text2.Trim(new char[]
|
|
{
|
|
'\r'
|
|
}).Split(new char[]
|
|
{
|
|
'='
|
|
});
|
|
if (array2.Length == 2 && array2[0].ToLower() == "filelength")
|
|
{
|
|
num = array2[1].ToInt64();
|
|
}
|
|
}
|
|
}
|
|
long num2 = num / blockAmount + 1L;
|
|
string path = targetFileName + ".temp";
|
|
string text3 = Path.Combine(targetFilePath, path);
|
|
using (FileStream fileStream = File.Open(text3, FileMode.Create))
|
|
{
|
|
long num3 = 0L;
|
|
while (num3 < num2 && !flag)
|
|
{
|
|
long pOffset = num3 * blockAmount;
|
|
FileUpDownInfo fileUpDownInfo = PublicFunction.DeserializeObject<FileUpDownInfo>(apclient.Service.DownloadBtye(sourceFilePath, sourceFileName, pOffset, blockAmount.ToInt()));
|
|
if (fileUpDownInfo.HasErr)
|
|
{
|
|
break;
|
|
}
|
|
byte[] fileContent = fileUpDownInfo.FileContent;
|
|
fileStream.Write(fileContent, 0, fileContent.Length);
|
|
int num4;
|
|
if (num2 > 0L)
|
|
{
|
|
num4 = (int)decimal.Floor(num3 / (num2 * 1m) * 100m);
|
|
}
|
|
else
|
|
{
|
|
num4 = 100;
|
|
}
|
|
if (num4 > 100)
|
|
{
|
|
num4 = 100;
|
|
}
|
|
num3 += 1L;
|
|
}
|
|
fileStream.Flush();
|
|
fileStream.Close();
|
|
if (File.Exists(text))
|
|
{
|
|
File.Delete(text);
|
|
}
|
|
File.Move(text3, text);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static DownloadErr WebUpLoadFile(FileUpDownInfo upFileInfo)
|
|
{
|
|
DownloadErr UploadFileErr = new DownloadErr();
|
|
|
|
bool flag = false;
|
|
string sourceFilePath = upFileInfo.SourceFilePath;
|
|
string sourceFileName = upFileInfo.SourceFileName;
|
|
string text = Path.Combine(sourceFilePath, sourceFileName);
|
|
try
|
|
{
|
|
long blockAmount = 512000L;
|
|
|
|
UploadFileErr.UpFilePath = upFileInfo.TargetFullPath;
|
|
UploadFileErr.SourceFilePath = upFileInfo.SourceFullPath;
|
|
FileInfo fileInfo = new FileInfo(text);
|
|
if (fileInfo == null)
|
|
{
|
|
UploadFileErr.HasErr = true;
|
|
UploadFileErr.Err = string.Format("文件{0}不存在!!!", text);
|
|
}
|
|
else
|
|
{
|
|
APClient apclient;
|
|
if (!string.IsNullOrEmpty(upFileInfo.ServerIP))
|
|
{
|
|
apclient = new APClient(upFileInfo.ServerIP);
|
|
}
|
|
else
|
|
{
|
|
apclient = new APClient();
|
|
}
|
|
using (FileStream fileStream = fileInfo.OpenRead())
|
|
{
|
|
bool flag2 = false;
|
|
bool isFirst = true;
|
|
fileStream.Position = 0L;
|
|
while (!flag)
|
|
{
|
|
int num = (int)(fileStream.Length - fileStream.Position);
|
|
if ((long)num > blockAmount)
|
|
{
|
|
byte[] array = new byte[blockAmount];
|
|
upFileInfo.IsFirst = isFirst;
|
|
upFileInfo.IsLast = false;
|
|
fileStream.Read(array, 0, (int)blockAmount);
|
|
upFileInfo.FileContent = array;
|
|
upFileInfo.Offset = fileStream.Position;
|
|
string text2 = apclient.Service.UploadFileByte(PublicFunction.SerializeObject(upFileInfo));
|
|
if (!string.IsNullOrEmpty(text2))
|
|
{
|
|
UploadFileErr.HasErr = true;
|
|
UploadFileErr.Err = text2;
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
byte[] array = new byte[num];
|
|
upFileInfo.IsFirst = isFirst;
|
|
upFileInfo.IsLast = true;
|
|
fileStream.Read(array, 0, num);
|
|
upFileInfo.FileContent = array;
|
|
upFileInfo.Offset = fileStream.Position;
|
|
string text3 = apclient.Service.UploadFileByte(PublicFunction.SerializeObject(upFileInfo));
|
|
if (!string.IsNullOrEmpty(text3))
|
|
{
|
|
UploadFileErr.HasErr = true;
|
|
UploadFileErr.Err = text3;
|
|
break;
|
|
}
|
|
flag2 = true;
|
|
}
|
|
int num2;
|
|
if (fileStream.Length > 0L)
|
|
{
|
|
num2 = (int)decimal.Floor(fileStream.Position / (fileStream.Length * 1m) * 100m);
|
|
}
|
|
else
|
|
{
|
|
num2 = 100;
|
|
}
|
|
if (num2 > 100)
|
|
{
|
|
num2 = 100;
|
|
}
|
|
isFirst = false;
|
|
if (flag2)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
fileStream.Close();
|
|
}
|
|
}
|
|
return UploadFileErr;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 上传下载文件传输时通用类
|
|
public class FileUpDownInfo
|
|
{
|
|
//
|
|
// 摘要:
|
|
// 目标文件名
|
|
public string TargetFileName
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 目标文件路径
|
|
public string TargetFilePath
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 源文件路径
|
|
public string SourceFilePath
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 源文件名称
|
|
public string SourceFileName
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 源全路径
|
|
public string SourceFullPath
|
|
{
|
|
get
|
|
{
|
|
return Path.Combine(SourceFilePath, SourceFileName);
|
|
}
|
|
set
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(value))
|
|
{
|
|
SourceFilePath = Path.GetDirectoryName(value);
|
|
SourceFileName = Path.GetFileName(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 目标全路径
|
|
public string TargetFullPath
|
|
{
|
|
get
|
|
{
|
|
return Path.Combine(TargetFilePath, TargetFileName);
|
|
}
|
|
set
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(value))
|
|
{
|
|
TargetFilePath = Path.GetDirectoryName(value);
|
|
TargetFileName = Path.GetFileName(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 首次上传(断点续传)
|
|
public bool IsFirst
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 最后一次上传(断点续传)
|
|
public bool IsLast
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 文件块实体
|
|
public byte[] FileContent
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 文件块位置
|
|
public long Offset
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 是否存在错误
|
|
public bool HasErr
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 错误信息
|
|
public string Err
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 文件下传下载协议
|
|
public UpOrDownType UpType
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 服务器IP
|
|
public string ServerIP
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 登录服务器用户帐号
|
|
public string UserID
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 登录服务器密码
|
|
public string Password
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 是否启用身份验证
|
|
public bool Credentials
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public FileUpDownInfo()
|
|
{
|
|
UpType = UpOrDownType.WebService;
|
|
Credentials = true;
|
|
TargetFileName = "";
|
|
TargetFilePath = "";
|
|
SourceFilePath = "";
|
|
SourceFileName = "";
|
|
}
|
|
}
|
|
|
|
//
|
|
// 摘要:
|
|
// 文件下传下载协议
|
|
public enum UpOrDownType
|
|
{
|
|
//
|
|
// 摘要:
|
|
// WebService的方式
|
|
WebService,
|
|
//
|
|
// 摘要:
|
|
// FTP方式
|
|
FTP
|
|
}
|
|
}
|