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:\Users\AmyZeng\Desktop\1.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();

原文地址:https://www.cnblogs.com/zengwangjing/p/10676091.html

时间: 2024-11-04 03:35:00

MemoryStream请求与接收的相关文章

python通过get方式,post方式发送http请求和接收http响应-urllib urllib2

python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/[email protected]/blog/static/132229655201231085444250/ 测试用CGI,名字为test.py,放在apache的cgi-bin目录下:#!/usr/bin/pythonimport cgidef main():     print "Content-t

linux原始套接字(2)-icmp请求与接收

一.概述                                                    上一篇arp请求使用的是链路层的原始套接字.icmp封装在ip数据报里面,所以icmp请求可以直接使用网络层的原始套接字,即socket()第一个参数是PF_INET.如下: 1 sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP); icmp报文不同的类型有不同的格式,我们以icmp回显请求和会显应答报文格式(即ping程序使用的报文类型)

linux原始套接字(1)-arp请求与接收

一.概述                                                   以太网的arp数据包结构: arp结构op操作参数:1为请求,2为应答. 常用的数据结构如下: 1.物理地址结构位于netpacket/packet.h 1 struct sockaddr_ll 2 { 3 unsigned short int sll_family; 4 unsigned short int sll_protocol; 5 int sll_ifindex; 6 unsi

struts2 请求参数接收

1. 采用基本类型接受请求参数(get/post)在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名的属性.请求路径:http://localhost:8080/action/register.action?id=33 public class HelloWorldAction { private Integer id; public Integer getId() { return id; } public void setId(Integer id)

post请求和接收

请求 String url = ""; try { JSONObject jsonParam = new JSONObject(); jsonParam.put("type", type); String sign = Md5Util.string2MD5(jsonParam.toString()); jsonParam.put("sign", sign); DefaultHttpClient httpClient = new DefaultHt

JAVA发送HttpClient请求及接收请求结果过程

下面为本人自己在工作中写业务代码的,并不是通用的,只供自己下次使用时能有个查找地,及正处在困扰中的程序员借鉴. 推荐好点博客给大家看看: http://blog.sina.com.cn/s/blog_75a8cfac01013aim.html http://blog.csdn.net/fireelement/article/details/2497136 http://www.2cto.com/kf/201206/136879.html http://284772894.iteye.com/bl

webapi 参数的请求和接收

数据传递和接收1.数据头为User-Agent: FiddlerAccept: application/xml; charset=utf-8Host: localhost:1258Content-Length: 26Content-Type: application/x-www-form-urlencoded; charset=UTF-8sign: 9f89c84a559f573636a47ff8daed0d335的时候 数据格式为 UserName=张三1&Pwd=admin 2User-Ag

HTTP请求与接收get/post方式

//get方式 public string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); request.Method = "GET"; request.Content

vue项目中mock数据get请求参数接收问题以及常规post参数写法

1.1前端vue组件内写法 this.$axios({ method:"get", url:"/news/index", data:{ product_type:'product' } }).then((res)=>{ //请求成功返回的数据 console.log(res); this.newsListShow = res.data.data.datalist; this.product_type=res.data.data.product_type; })