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