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 readonly object _obj = new object();
private static string _lfData;
public static string LFData
{
get
{
if (string.IsNullOrEmpty(_lfData))
{
_lfData = Tools.GetSetting("lfData");
}
return _lfData;
}
}
// http://localhost/ExportMap/tile0/12/3338/1671.png?path=2d\tiles\0102
private static byte[] _emptyPng;
public static byte[] EmptyPng
{
get
{
if (null == _emptyPng)
{
lock (_obj)
{
if (null == _emptyPng)
{
string file = Tools.BaseDir + "Sources\\empty.png";
_emptyPng = ReadFileToBytes(file);
}
}
}
return _emptyPng;
}
}
// http://127.0.0.1/ExportMap/terra0/12/6822/2744.terrain?v=1.1.0&path=3d\terrain\dem\t
private static byte[] _emptyTerrain;
public static byte[] EmptyTerrain
{
get
{
if (null == _emptyTerrain)
{
lock (_obj)
{
if (null == _emptyTerrain)
{
string file = Tools.BaseDir + "Sources\\empty.terrain";
_emptyTerrain = ReadFileToBytes(file);
}
}
}
return _emptyTerrain;
}
}
#endregion
///
/// 获取地形图层文件
///
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", "application/json");
}
///
/// 获取地形文件
///
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");
}
///
/// 获取地形文件,不存在返回空文件
///
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(EmptyTerrain, y + ".terrain");
}
return DownloadFile(file, y + ".terrain");
}
///
/// 获取影像文件
///
public static HttpResponseMessage GetTile(HttpRequestMessage Request, string path, int z, int x, int y)
{
string basePath = Path.Combine(LFData, path, z.ToString());
if (!Directory.Exists(basePath))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "路径不存在!"));
}
string file = basePath + "\\" + x + "\\" + y + ".png";
if (!File.Exists(file))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "文件不存在!"));
}
return DownloadFile(file, y + ".png", "image/png");
}
///
/// 获取影像文件,不存在返回空文件
///
public static HttpResponseMessage GetTile0(HttpRequestMessage Request, string path, int z, int x, int y)
{
string basePath = Path.Combine(LFData, path, z.ToString());
if (!Directory.Exists(basePath))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "路径不存在!"));
}
string file = basePath + "\\" + x + "\\" + y + ".png";
if (!File.Exists(file))
{
return DownloadFile(EmptyPng, y + ".png", "image/png");
}
return DownloadFile(file, y + ".png", "image/png");
}
///
/// 下载文件
///
public static HttpResponseMessage DownloadFile(string file, string fileName, string mediaType = "application/octet-stream")
{
//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(mediaType);
//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;
}
///
/// 下载文件
///
public static HttpResponseMessage DownloadFile(byte[] bytes, string fileName, string mediaType = "application/octet-stream")
{
MemoryStream ms = new MemoryStream(bytes);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(ms);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = HttpUtility.UrlEncode(fileName);
response.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
//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;
}
#region 工具类
///
/// 读取文件至byte[]
///
public static byte[] ReadFileToBytes(string file)
{
using (FileStream fs = new FileStream(file, FileMode.Open))
{
int len = (int)fs.Length;
byte[] bytes = new byte[len];
fs.Read(bytes, 0, len);
fs.Flush();
return bytes;
}
}
///
/// Get请求
///
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;
}
///
/// Post请求
/// string data = string.Format("token={0}&f=json&where={1}", Token, "");
///
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;
}
#endregion
}
}