管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-09-23 6de2aa3b55fb547ca4c28c5be0819d9a5ff8a96c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Web.Http;
 
namespace ExportMap.cs
{
    public class WebUtils
    {
        #region LFData
        private static string _lfData;
 
        public static string LFData
        {
            get
            {
                if (string.IsNullOrEmpty(_lfData))
                {
                    _lfData = Tools.GetSetting("lfData");
                }
 
                return _lfData;
            }
        }
        #endregion
 
        /// <summary>
        /// 获取地形图层文件
        /// </summary>
        public static HttpResponseMessage GetLayerJson(HttpRequestMessage Request, string path)
        {
            string basePath = Path.Combine(LFData, path);
            if (!Directory.Exists(basePath))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "路径不存在!"));
            }
 
            string file = basePath + "\\layer.json";
            if (!File.Exists(file))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "文件不存在!"));
            }
 
            return DownloadFile(file, "layer.json");
        }
 
        /// <summary>
        /// 获取地形文件
        /// </summary>
        public static HttpResponseMessage GetTerrain(HttpRequestMessage Request, string path, int z, int x, int y)
        {
            string basePath = Path.Combine(LFData, path);
            if (!Directory.Exists(basePath))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "路径不存在!"));
            }
 
            string file = basePath + "\\" + z + "\\" + x + "\\" + y + ".terrain";
            if (!File.Exists(file))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "文件不存在!"));
            }
 
            return DownloadFile(file, y + ".terrain");
        }
 
        /// <summary>
        /// 获取地形文件,不存在返回空文件
        /// </summary>
        public static HttpResponseMessage GetTerrain0(HttpRequestMessage Request, string path, int z, int x, int y)
        {
            string basePath = Path.Combine(LFData, path);
            if (!Directory.Exists(basePath))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "路径不存在!"));
            }
 
            string file = basePath + "\\" + z + "\\" + x + "\\" + y + ".terrain";
            if (!File.Exists(file))
            {
                return DownloadFile(Tools.BaseDir + "\\Sources\\empty.terrain", y + ".terrain");
            }
 
            return DownloadFile(file, y + ".terrain");
        }
 
        /// <summary>
        /// 下载文件
        /// </summary>
        public static HttpResponseMessage DownloadFile(string file, string fileName)
        {
            //string fileName = Path.GetFileName(file);
            //FileStream stream = new FileStream(file, FileMode.Open);
            FileStream stream = File.OpenRead(file);
 
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StreamContent(stream);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = HttpUtility.UrlEncode(fileName);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
 
            //response.Content.Headers.Add("Cache-Control", "max-age=2592000"); // 缓存1个月
            response.Content.Headers.Expires = DateTimeOffset.Now.AddMonths(1); // new DateTimeOffset(DateTime.Now.AddMonths(1));
 
            return response;
        }
 
        /// <summary>
        /// Get请求
        /// </summary>
        public static string GetData(string url)
        {
            /*WebClient wc = new WebClient();
            Byte[] pageData = wc.DownloadData(url);
 
            Encoding coding = Encoding.GetEncoding("UTF-8");
            string str = coding.GetString(pageData);
 
            return str;*/
 
            Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
 
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string str = reader.ReadToEnd();
            reader.Close();
 
            return str;
        }
 
        /// <summary>
        /// Post请求
        /// string data = string.Format("token={0}&f=json&where={1}", Token, "");
        /// </summary>
        public static string PostData(string url, string data)
        {
            Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
 
            if (data != null)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                request.ContentLength = bytes.Length;
                Stream outstream = request.GetRequestStream();
                outstream.Write(bytes, 0, bytes.Length);
                outstream.Flush();
                outstream.Close();
            }
 
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string result = reader.ReadToEnd();
            reader.Close();
 
            return result;
        }
    }
}