程序需要管理員權限,vs需要管理員打開
首次運行需要執行以下命令注冊URL(管理員命令行)
netsh advfirewall firewall add rule name="FileShare" dir=in action=allow protocol=TCP localport=8000
ipconfig | findstr "IPv4"
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Linq;class Program
{static void Main(){string path = @"D:\8000"; // 共享目錄int port = 8000;HttpListener listener = new HttpListener();listener.Prefixes.Add($"http://+:{port}/");listener.Start();Console.WriteLine($"服務器已啟動: http://{GetLocalIP()}:{port}");while (true){var context = listener.GetContext();if (context.Request.HttpMethod == "POST"){ProcessUploadRequest(context, path);}else{ProcessRequest(context, path);}}}static void ProcessUploadRequest(HttpListenerContext context, string rootPath){try{// 獲取上傳文件名string filename = context.Request.Headers["X-FileName"] ?? Path.GetRandomFileName();string filePath = Path.Combine(rootPath, filename);using (FileStream fs = new FileStream(filePath, FileMode.Create)){context.Request.InputStream.CopyTo(fs);}SendResponse(context, HttpStatusCode.Created, "文件上傳成功");}catch (Exception ex){SendResponse(context, HttpStatusCode.InternalServerError, $"上傳失敗: {ex.Message}");}}static void ProcessRequest(HttpListenerContext context, string rootPath){try{string requestPath = context.Request.Url.LocalPath.TrimStart('/');string fullPath = Path.Combine(rootPath, requestPath);// 處理文件下載if (File.Exists(fullPath)){using (FileStream fs = File.OpenRead(fullPath)){context.Response.ContentType = GetMimeType(Path.GetExtension(fullPath));context.Response.AddHeader("Content-Disposition", $"attachment; filename=\"{Path.GetFileName(fullPath)}\"");fs.CopyTo(context.Response.OutputStream);}}// 處理目錄瀏覽else if (Directory.Exists(fullPath)){string directoryList = GenerateDirectoryListing(fullPath, context.Request.Url.AbsoluteUri);byte[] buffer = Encoding.UTF8.GetBytes(directoryList);context.Response.ContentType = "text/html; charset=utf-8";context.Response.OutputStream.Write(buffer, 0, buffer.Length);}else{SendResponse(context, HttpStatusCode.NotFound, "資源不存在");}}catch (Exception ex){SendResponse(context, HttpStatusCode.InternalServerError, $"處理請求失敗: {ex.Message}");}finally{context.Response.Close();}}static string GenerateDirectoryListing(string path, string baseUrl){var sb = new StringBuilder();sb.Append("<html><head><title>文件列表</title></head><body>");sb.Append($"<h1>文件列表 - {path}</h1><ul>");// 添加返回上級目錄鏈接if (Directory.GetParent(path) != null){sb.Append($"<li><a href='{baseUrl}../'>[上級目錄]</a></li>");}// 遍歷目錄foreach (var dir in Directory.GetDirectories(path)){string dirName = Path.GetFileName(dir);sb.Append($"<li><a href='{baseUrl}{dirName}/'>[目錄] {dirName}/</a></li>");}// 遍歷文件foreach (var file in Directory.GetFiles(path)){string fileName = Path.GetFileName(file);sb.Append($"<li><a href='{baseUrl}{fileName}'>{fileName}</a></li>");}sb.Append("</ul></body></html>");return sb.ToString();}static void SendResponse(HttpListenerContext context, HttpStatusCode statusCode, string message){byte[] buffer = Encoding.UTF8.GetBytes(message);context.Response.StatusCode = (int)statusCode;context.Response.ContentType = "text/plain; charset=utf-8";context.Response.OutputStream.Write(buffer, 0, buffer.Length);}/*** * MIME類型映射類* MIME(Multipurpose Internet Mail Extensions)? * 類型是一種標準化的方式,用于描述互聯網上傳輸的內容類型(例如文本、圖像、視頻等)。* 它的核心作用是告訴瀏覽器或客戶端?如何正確處理文件?(例如直接顯示、下載、調用外部程序打開等)*/static string GetMimeType(string extension){var mimeTypes = new Dictionary<string, string>{{ ".txt", "text/plain" },{ ".pdf", "application/pdf" },{ ".doc", "application/msword" },{ ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },{ ".xls", "application/vnd.ms-excel" },{ ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },{ ".png", "image/png" },{ ".jpg", "image/jpeg" },{ ".jpeg", "image/jpeg" },{ ".gif", "image/gif" },{ ".zip", "application/zip" }};return mimeTypes.TryGetValue(extension.ToLower(), out string mime) ? mime : "application/octet-stream";}static string GetLocalIP(){return Dns.GetHostEntry(Dns.GetHostName()).AddressList.First(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString();}
}
文件上傳
curl.exe -X POST -H “X-FileName: Git-2.46.2-64-bit.exe” --data-binary “@C:\Users\Ins\Downloads\Git-2.46.2-64-bit.exe” http://192.168.1.242:8000/