weixin_41764162 2020-01-09 13:45 采纳率: 100%
浏览 631
已采纳

winfrom 通过multipart/form-data方式 上传EXCEL文件,发生错误

static void FileUpload(string m_fileNamePath, string wenjianmin)
{
string Boundary = "wojuedezhgexiangmushigekeng";

        //构造请求参数
        Dictionary<string, string> PostInfo = new Dictionary<string, string>();
        //PostInfo.Add("sequenceNo ", "A0002");
        //PostInfo.Add("type", "application/vnd.ms-excel");
        //PostInfo.Add("file", "");
        //PostInfo.Add("filename", wenjianmin);

        //构造POST请求体
        StringBuilder PostContent = new StringBuilder("--" + Boundary);
        byte[] ContentEnd = Encoding.UTF8.GetBytes("--" + Boundary + "--\r\n");//请求体末尾,后面会用到
                                                                               //组成普通参数信息
        foreach (KeyValuePair<string, string> item in PostInfo)
        {
            PostContent.Append("\r\n")
                    .Append("Content-Disposition: form-data; name=\"")
                    .Append(item.Key + "\"").Append("\r\n")
                    .Append("\r\n").Append(item.Value).Append("\r\n")
                    .Append("--").Append(Boundary);
        }
        //转换为二进制数组,后面会用到
        byte[] PostContentByte = Encoding.UTF8.GetBytes(PostContent.ToString());

        //文件信息
        byte[] UpdateFile = File2Bytes(m_fileNamePath);//转换为二进制
        StringBuilder FileContent = new StringBuilder();
        FileContent.Append("\r\n")
                .Append("Content-Disposition:form-data; name=\"")
                .Append("flie" + "\"; ")
                .Append("filename=\"")
                .Append(wenjianmin + "\"")
                .Append("\r\n")
                .Append("Content-Type: application/vnd.ms-excel")
                .Append("\r\n")
                .Append("\r\n");
        byte[] FileContentByte = Encoding.UTF8.GetBytes(FileContent.ToString());

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_address);
        request.Method = "POST";
        request.Timeout = 100000;
        //这里确定了分隔符是什么
        request.Headers.Add("Cookie", "JSESSIONID=19FCE6CF428E78732830248719E3836A");
        //request.Headers.Add("Accept-Encoding", " gzip, deflate");

        request.ContentType = "multipart/form-data;boundary=" + Boundary;
        //request.Cookie = "";
        //定义请求流
        Stream myRequestStream = request.GetRequestStream();
        myRequestStream.Write(PostContentByte, 0, PostContentByte.Length);//写入参数
        myRequestStream.Write(FileContentByte, 0, FileContentByte.Length);//写入文件信息
        myRequestStream.Write(UpdateFile, 0, UpdateFile.Length);//文件写入请求流中
        myRequestStream.Write(ContentEnd, 0, ContentEnd.Length);//写入结尾                



        HttpWebResponse res;
        try
        {
            res = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException ex)
        {
            res = (HttpWebResponse)ex.Response;
        }
        //StreamReader sr = new StreamReader(res.GetResponseStream(), strEncode);
        //strHtml = sr.ReadToEnd();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();


        //获取返回值
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

        string retString = myStreamReader.ReadToEnd();
        myRequestStream.Close();
        myStreamReader.Close();
        myResponseStream.Close();
    }


            以下是抓包截图

            ![图片说明](https://img-ask.csdn.net/upload/202001/09/1578548657_696695.png)

            返回错误信息
            {"timestamp":"2020-01-09 13:40:35","status":500,"error":"Internal Server Error","message":"Failed to parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly","path":"/lab/calibrator/uploadReport"}
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-01-09 13:47
    关注

    估计你multipart数据编码有问题,直接用 webclient.upload 上传。感觉你直接写二进制了,没有做base64编码

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?