using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.ServiceModel;
|
using System.IO;
|
using System.Threading;
|
using System.Configuration;
|
namespace CY_DocumentFileWCFService
|
{
|
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
|
public class FileService:IFileService
|
{
|
string root = string.Empty;
|
string serverFileRootPath = string.Empty;
|
private const int BufferLen = 4096;
|
private static Dictionary<string, int> _uploadInfoDict = new Dictionary<string, int>();
|
private static object _lockObj = new object();
|
|
|
public FileService()
|
{
|
root = AppDomain.CurrentDomain.BaseDirectory + "\\Temp\\";
|
if (!System.IO.Directory.Exists(root))
|
System.IO.Directory.CreateDirectory(root);
|
serverFileRootPath = ConfigurationManager.AppSettings["ServerFileRootPath"].ToString();
|
if (!System.IO.Directory.Exists(serverFileRootPath))
|
System.IO.Directory.CreateDirectory(serverFileRootPath);
|
}
|
|
public void UploadFile(FileData request)
|
{
|
string filePath = root + request.FilePath;
|
if (FileHelper.IsExists(filePath))
|
{
|
FileHelper.DelFile(filePath);
|
}
|
FileHelper.CreateDirtory(filePath);
|
string fileName = request.FileName;
|
Stream sourceStream = request.FileDataStream;
|
FileStream targetStream = null;
|
bool isSuccess = false;
|
using (targetStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
|
{
|
isSuccess=CopyStream(request.FileUniqueID, sourceStream, targetStream);
|
}
|
if (isSuccess)
|
{
|
string targetFile = serverFileRootPath + request.FilePath;
|
CopyFile(filePath, targetFile);
|
}
|
targetStream.Close();
|
sourceStream.Close();
|
}
|
|
private void CopyFile(string sroceFile, string targetFile)
|
{
|
FileHelper.CreateDirtory(targetFile);
|
File.Copy(sroceFile, targetFile,true);
|
File.Delete(sroceFile);
|
}
|
|
bool CopyStream(string id, Stream fromstream, Stream tostream)
|
{
|
bool isSuccess = true;
|
byte[] buffer = new byte[BufferLen];
|
int count = 0, offset = 0;
|
try
|
{
|
while ((count = fromstream.Read(buffer, offset, BufferLen - offset)) > 0)
|
{
|
offset += count;
|
if (offset == BufferLen)
|
{
|
tostream.Write(buffer, 0, offset);
|
offset = 0;
|
SetFileUploadInfo(id, (int)(tostream.Position));
|
}
|
}
|
if (offset > 0)
|
tostream.Write(buffer, 0, offset);
|
SetFileUploadInfo(id, (int)(tostream.Position));
|
}
|
catch (Exception ex)
|
{
|
isSuccess = false;
|
}
|
finally
|
{
|
fromstream.Dispose();
|
tostream.Dispose();
|
}
|
|
return isSuccess;
|
}
|
|
private void SetFileUploadInfo(string id, int savedFileCount)
|
{
|
lock (_lockObj)
|
{
|
if (_uploadInfoDict.ContainsKey(id))
|
_uploadInfoDict[id] = savedFileCount;
|
else
|
_uploadInfoDict.Add(id, savedFileCount);
|
}
|
}
|
|
public Stream DownLoadFile(string filePath, long offset = 0, long count = 0)
|
{
|
//string targetFile = root + filePath;
|
//FileHelper.CreateDirtory(targetFile);
|
string sroceFile = serverFileRootPath + filePath;
|
//CopyFile(sroceFile, targetFile);
|
string fileName = Path.GetFileName(sroceFile);
|
|
string path = sroceFile;
|
if (!File.Exists(path))
|
{
|
throw new FaultException("没找到文件:" + fileName);
|
}
|
FileInfo file = new FileInfo(path);
|
if (count == 0)
|
{
|
count = file.Length - offset;
|
}
|
Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
return stream;
|
}
|
|
public int GetUploadFileInfo(string id)
|
{
|
if (_uploadInfoDict.ContainsKey(id))
|
return _uploadInfoDict[id];
|
else
|
return 0;
|
}
|
}
|
}
|