我将要编写一个服务器应用程序,它应该能够从多个来源接收大文件(与所有其他FTP客户端/服务器应用程序一样安静)。
但是我不确定哪种方法最好,需要一些建议。
客户端会将XML数据发送到服务器,如下所示:
<Data xmlns="http://schemas.datacontract.org/2004/07/DataFiles" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Category>General</Category>
<Files>
<DataFile>
<Extension>.txt</Extension>
<Filename>test4</Filename>
<Bytes>"Some binary data"</Bytes>
</DataFile>
</Files>
</Data>
我最初创建一个HTTPListener作为服务器,但是在服务器端处理大型文件时似乎很费劲(基本上是因为上下文是作为一个没有分段的数据包接收的,并且当服务器对接收到的XML数据进行反序列化时,它将加载将其存储到内存中,这对于大型文件来说是不行的。
然后,我继续使用TcpListener向下移动一层,这在大型文件上似乎可以正常工作,因为它们是零散发送的,但是当接收到请求时,我需要做很多工作来将包附加到服务器端。
我也很可能跳过WCF,但是由于缺乏这项技术的经验,我不得不离开该方法。
你会怎么做?您将使用伟大的.NET工具箱中的哪个.NET工具创建FTP服务器/客户端?
关于TcpListeners等有很多线程,这不是我在这里寻求的。我需要有关应该采用哪种方法的建议以及最佳实践。
编辑:忘记提及它背后的想法更像是FTP代理(客户端将文件发送到服务器>服务器在本地存储文件>服务器将其发送到第三部分位置>服务器将文件分割到第三部分时清除本地存储的文件成功完成)。
编辑17-11-15:
这是我如何执行HTTP服务器的示例代码:
public class HttpServer
{
protected readonly HttpListener HttpListener = new HttpListener();
protected HttpServer(IEnumerable<string> prefixes)
{
HttpListener.Prefixes.Add(prefix);
}
public void Start()
{
while (HttpListener.IsListening && Running)
{
var result = HttpListener.BeginGetContext(ContextReceived, HttpListener);
if (WaitHandle.WaitAny(new[] {result.AsyncWaitHandle, _shutdown}) == 0)
return;
}
}
protected object ReadRequest(HttpListenerRequest request)
{
using (var input = request.InputStream)
using (var reader = new StreamReader(input, request.ContentEncoding))
{
var data = reader.ReadToEnd();
return data;
}
}
protected void ContextReceived(IAsyncResult ar)
{
HttpListenerContext context = null;
HttpListenerResponse response = null;
try
{
var listener = ar.AsyncState as HttpListener;
if (listener == null) throw new InvalidCastException("ar");
context = listener.EndGetContext(ar);
response = context.Response;
switch (context.Request.HttpMethod)
{
case WebRequestMethods.Http.Post:
// Parsing XML data with file at LARGE byte[] as one of the parameter, seems to struggle here...
break;
default:
//Send MethodNotAllowed response..
break;
}
response.Close();
}
catch(Exception ex)
{
//Do some properly exception handling!!
}
finally
{
if (context != null)
{
context.Response.Close();
}
if (response != null)
response.Close();
}
}
}
客户端正在使用:
using (var client = new WebClient())
{
GetExtensionHeaders(client.Headers);
client.Encoding = Encoding.UTF8;
client.UploadFileAsync(host, fileDialog.FileName ?? "Test");
client.UploadFileCompleted += ClientOnUploadFileCompleted;
client.UploadProgressChanged += ClientOnUploadProgressChanged;
}
请注意,客户端应该将数据(以XML格式)发送到服务器,服务器将如前所述反序列化(使用文件流服务器端)接收到的数据。
这是我的TcpServer示例:
public class TcpServer
{
protected TcpListener Listener;
private bool _running;
public TcpServer(int port)
{
Listener = new TcpListener(IPAddress.Any, port);
Console.WriteLine("Listener started @ {0}:{1}", ((IPEndPoint)Listener.LocalEndpoint).Address, ((IPEndPoint)Listener.LocalEndpoint).Port);
_running = true;
}
protected readonly ManualResetEvent TcpClientConnected = new ManualResetEvent(false);
public void Start()
{
while (_running)
{
TcpClientConnected.Reset();
Listener.BeginAcceptTcpClient(AcceptTcpClientCallback, Listener);
TcpClientConnected.WaitOne(TimeSpan.FromSeconds(5));
}
}
protected void AcceptTcpClientCallback(IAsyncResult ar)
{
try
{
var listener = ar.AsyncState as TcpListener;
if (listener == null) return;
using (var client = listener.EndAcceptTcpClient(ar))
{
using (var stream = client.GetStream())
{
//Append or create to file stream
}
}
//Parse XML data received?
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
TcpClientConnected.Set();
}
}
}
创建一个新的空MVC应用程序
接下来,将新的控制器添加到Controllers
文件夹中,
using System.Web;
using System.Web.Mvc;
namespace UploadExample.Controllers
{
public class UploadController : Controller
{
public ActionResult File(HttpPostedFileBase file)
{
file.SaveAs(@"c:\FilePath\" + file.FileName);
}
}
}
现在,您上传文档所需要做的就是将其作为多部分表单数据发布到您的网站...
void Main()
{
string fileName = @"C:\Test\image.jpg";
string uri = @"http://localhost/Upload/File";
string contentType = "image/jpeg";
Http.Upload(uri, fileName, contentType);
}
public static class Http
{
public static void Upload(string uri, string filePath, string contentType)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
string formdataTemplate = "Content-Disposition: form-data; name=file; filename=\"{0}\";\r\nContent-Type: {1}\r\n\r\n";
string formitem = string.Format(formdataTemplate, Path.GetFileName(filePath), contentType);
byte[] formBytes = Encoding.UTF8.GetBytes(formitem);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.SendChunked = true;
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
requestStream.Write(formBytes, 0, formBytes.Length);
byte[] buffer = new byte[1024*4];
int bytesLeft;
while ((bytesLeft = fileStream.Read(buffer, 0, buffer.Length)) > 0) requestStream.Write(buffer, 0, bytesLeft);
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
}
Console.WriteLine ("Success");
}
}
如果遇到问题,请编辑您的Web.Config文件,很可能是您达到了请求长度限制...
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5" maxRequestLength="1048576"/>
</system.web>
我错过了(但现在已编辑)的另一件事是webrequest上的send chunked属性本身。
request.SendChunked = true;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句