MemoryStream请求与接收

Posted zengwangjing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MemoryStream请求与接收相关的知识,希望对你有一定的参考价值。

//流请求

static void Main(string[] args)

{
Console.WriteLine("Hello World!");
//Console.ReadLine();
List<EB_LOG> logs=new List<EB_LOG>(){
new EB_LOG (){ id="11111", name="22222"},
new EB_LOG (){ id="11111", name="22222"},
};
MemoryStream fs = ToExcel<EB_LOG>(logs);
using (var httpClient = new HttpClient())
{
//1.创建文件流
//FileStream fsRead = new FileStream(@"C:UsersAmyZengDesktop1.txt", FileMode.Open);
//2.创建缓冲区,正常情况下,是不会直接等于文件大小的。这里只有读,所以就这么干了。
byte[] byteArray = fs.ToArray();
fs.Read(byteArray, 0, byteArray.Length);
//3.开始读取, 返回值是读取到的长度。
//int r = fsRead.Read(bytes, 0, bytes.Lenght);

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:58278接收地址"));
webRequest.Method = "POST"; //POST
webRequest.ContentType = "application/x-xls";
webRequest.ContentLength = byteArray.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();

HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string sss = sr.ReadToEnd();
//4.关闭释放流
fs.Close();
fs.Dispose();

}
 
}

public static MemoryStream ToExcel<T>(List<T> list, string filePath = null)
{
var memoryStream = new MemoryStream();
 

IWorkbook workbook = new HSSFWorkbook();
string sheetName = typeof(T).Name;
ISheet sheet = workbook.CreateSheet(sheetName);
IRow headerRow = sheet.CreateRow(0);
Type elementType = typeof(T);
// handling header.

int headerIndex = 0;
elementType.GetProperties().ToList().ForEach(propInfo =>
{
ICell headerCell = headerRow.CreateCell(headerIndex);
headerIndex = headerIndex + 1;
headerCell.SetCellValue(propInfo.Name);


});
int rowIndex = 1;
foreach (T item in list)
{
IRow dataRow = sheet.CreateRow(rowIndex);
int rowcellIndex = 0;
elementType.GetProperties().ToList().ForEach(propInfo =>
{
ICell cell = dataRow.CreateCell(rowcellIndex);

string value = (propInfo.GetValue(item, null) ?? "").ToString();
cell.SetCellValue(value);
rowcellIndex++;
});
rowIndex++;
}


///storage/emulated/0/DCIM
//FileStream fs = new FileStream("/storage/emulated/0/DCIM/log.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite);

workbook.Write(memoryStream);

//fs.Write(memoryStream.ToArray(), 0, (int)memoryStream.Length);
 
//fs.Dispose();
workbook = null;

return memoryStream;
 }
 
 
 
//接收代码

string logpatch = “D:Logs”
if (!string.IsNullOrEmpty(logpatch))
{
if (!System.IO.Directory.Exists(logpatch))
{
System.IO.Directory.CreateDirectory(logpatch);
}
}
else
{

logpatch = System.AppDomain.CurrentDomain.BaseDirectory+ "Logs\";
if (!System.IO.Directory.Exists(logpatch))
{
System.IO.Directory.CreateDirectory(logpatch);
}
}

logpatch = logpatch + System.DateTime.Today.ToString("yyyyMMdd") + "\";
if (!System.IO.Directory.Exists(logpatch))
{
System.IO.Directory.CreateDirectory(logpatch);
}

byte[] bufferSteam = new byte[context.Request.InputStream.Length];
context.Request.InputStream.Read(bufferSteam, 0, bufferSteam.Length);
string strPath = logpatch + DateTime.Now.ToString("yyyyMMddHHmmsss")+"_" + strStoreTillid + ".xls";
FileStream fsSteam = new FileStream(strPath, FileMode.Create, FileAccess.Write);
BinaryWriter bwSteam = new BinaryWriter(fsSteam);
bwSteam.Write(bufferSteam);
bwSteam.Close();
fsSteam.Close();

 
 

以上是关于MemoryStream请求与接收的主要内容,如果未能解决你的问题,请参考以下文章

接收请求参数与 RestfulSpringMVC

Server是如何完成针对请求的监听接收与响应1

HttpURLConnection 发送PUT请求 json请求体 与服务端接收

HTTP请求与接收get/post方式

前端HTTP发POST请求携带参数与后端接口接收参数

前端HTTP发POST请求携带参数与后端接口接收参数