c#上传文件到服务端
来源:互联网 发布:软件研发管理系统 编辑:程序博客网 时间:2024/05/01 13:12
c#上传文件到服务器
本文主要介绍如何以文件流的方式上传本地文件到服务端:
环境介绍
- 所用语言:客户端C#,服务端Java+Spring框架
客户端代码
说明: 通过网页端调试可以发现,上传文件流时候,请求信息里面有
ContentType = "multipart/form-data;charset=utf-8;boundary=----WebKitFormBoundary" + boundary;
上述说明了服务端接受的数据类型以及boundary,boundary主要作用是帮助服务端接收的时候区分不同的参数,可以随机生成一段字符串,本文的生成方式采用的是
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
注意:对于每一个上传的参数前需要itemBoundaryBytes,所有参数完成后需要加上endBoundaryBytes
需要上传的参数,均为byte数组的形式,通过postStream.Write方法上传,顺序为itemBoundaryBytes+参数的bytes,最后为endBoundaryBytes;
最后通过HttpWebResponse res获得服务端的响应
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.MaximumResponseHeadersLength = 1024; request.Method = "POST"; string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线 request.ContentType = "multipart/form-data;charset=utf-8;boundary=----WebKitFormBoundary" + boundary; byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n------WebKitFormBoundary" + boundary + "\r\n"); byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n------WebKitFormBoundary" + boundary + "--\r\n"); int pos = path.LastIndexOf("\\"); string fileName = path.Substring(pos + 1); //请求头部信息 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName)); StringBuilder sbHeader2 = new StringBuilder("Content-Disposition:form-data;name=\"name\"\r\n\r\n"); byte[] namebyte = Encoding.UTF8.GetBytes(name); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); byte[] postHeaderBytes2 = Encoding.UTF8.GetBytes(sbHeader2.ToString()); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] bArr = new byte[fs.Length]; fs.Read(bArr, 0, bArr.Length); fs.Close(); Stream postStream = request.GetRequestStream(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bArr, 0, bArr.Length); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes2, 0, postHeaderBytes2.Length); postStream.Write(namebyte, 0, namebyte.Length); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); string header = "\r\n----WebKitFormBoundary" + boundary + "\r\n" + sbHeader.ToString() + " xx " + "\r\n----WebKitFormBoundary" + boundary + "\r\n" + sbHeader2.ToString() + "test234" + "\r\n----WebKitFormBoundary" + boundary + "--\r\n"; postStream.Close(); HttpWebResponse res; try { res = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { res = (HttpWebResponse)ex.Response; } StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8); string content = sr.ReadToEnd(); return content;
服务端代码
参数使用的是@RequestParm
@RequestMapping(method = RequestMethod.POST) public void upload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) { svc.getAppFdbService().uploadFile(name, file); }
0 0
- c#上传文件到服务端
- actionscript里面如何上传文件到服务端?
- 服务端签名后上传文件到oss
- c#服务端接收客户端的文件上传,可以实现到断点续传
- C#上传文件到ftp
- c#上传文件到服务器
- 客户端上传到服务端图片,服务端把图片保存到指定的文件中
- flex上传文件到服务端(python脚本)
- Tcp从Socket客户端上传文件到ServerSocket服务端
- Android 上传文件到服务端,并显示进度条
- 使用FormData来提交表单同时上传文件到服务端
- 服务端文件上传测试
- 文件上传 服务端代码
- C#上传文件到网站指定目录
- c# WebClient上传文件到远程服务器
- c# webDav上传文件到服务器
- post上传视频文件到服务端
- TCP 上传图片到服务端
- linux下IDE配置,winmanager配置问题
- 机器学习-统计学习方法概论
- STM8/STM32硬件I2C读取APDS9930程序代码
- 关于数组的一些知识点和API
- 每天一个linux命令(11):nl命令
- c#上传文件到服务端
- listview的优化
- 问题:w3c中clientheight的用法?
- JAVA、android开发常用英语汇总《上》
- some tips in Maya
- 使用Junit对spring controller进行单元测试与数据的分页
- 安装cocoapods及使用详解(持续更新)
- iOS tabbar 阴影线问题
- 分享pdf转换jpg格式的方法