qwj
2022-08-22 63f6c5de8832eaabf066fe9f9d492187f04011b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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;
        }
    }
}