栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Spring的MockMultipartHttpServletRequest?得到“未找到多部分边界”

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Spring的MockMultipartHttpServletRequest?得到“未找到多部分边界”

您需要设置边界。

这里对边界是什么有很好的解释http://codingdict.com/questions/144876

要解决您的问题,请尝试以下代码。

    import java.io.IOException;    import java.nio.file.Files;    import java.nio.file.Path;    import java.nio.file.Paths;    import org.apache.commons.lang.ArrayUtils;    import org.springframework.mock.web.MockHttpServletResponse;    import org.springframework.mock.web.MockMultipartFile;    import org.springframework.mock.web.MockMultipartHttpServletRequest;public class FileUploadTest {    public void testDoPost() throws IOException { Path path = Paths.get("c:\temp\test.zip"); byte[] data = Files.readAllBytes(path); MockMultipartFile file = new MockMultipartFile("test.zip", "test.zip",         "application/zip", data); MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest(); String boundary = "q1w2e3r4t5y6u7i8o9"; mockRequest.setContentType("multipart/form-data; boundary="+boundary); mockRequest.setContent(createFileContent(data,boundary,"application/zip","test.zip")); mockRequest.addFile(file); mockRequest.setMethod("POST"); mockRequest.setParameter("variant", "php"); mockRequest.setParameter("os", "mac"); mockRequest.setParameter("version", "3.4"); MockHttpServletResponse response = new MockHttpServletResponse(); new FileUpload().doPost(mockRequest, response);        }        public byte[] createFileContent(byte[] data, String boundary, String contentType, String fileName){ String start = "--" + boundary + "rn Content-Disposition: form-data; name="file"; filename=""+fileName+""rn"          + "Content-type: "+contentType+"rnrn";; String end = "rn--" + boundary + "--"; // correction suggested @butfly  return ArrayUtils.addAll(start.getBytes(),ArrayUtils.addAll(data,end.getBytes()));        }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464019.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号