我正在servlet中编写多部分文件上传测试用例,但是如上所述它抛出异常:
代码:
public class MessageUploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List uploadedItems = null;
try {
uploadedItems = upload.parseRequest(request);
upload.setSizeMax(10485760);
} catch (FileUploadException e) {
e.printStackTrace();
}}}
我参考了提及位置的写单元测试 How to use spring's MockMultipartHttpServletRequest? Getting "no multipartboundary was found"
例外:
2018-01-04 18:38:15错误MessageUploadServlet:157 - 处理 multipart / form-data请求失败。流意外结束 org.apache.commons.fileupload.FileUploadBase $ IOFileUploadException: 处理multipart / form-data请求失败。小溪结束了 出人意料地 org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:371) 在 org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126) 在 tm.servlet.MessageUploadServlet.doPost(MessageUploadServlet.java:74) 在 tm.servlet.MessageUploadServletTest.testServlet(MessageUploadServletTest.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at java.lang.reflect.Method.invoke(未知来源)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 引起: org.apache.commons.fileupload.MultipartStream $ MalformedStreamException: 流意外结束了 org.apache.commons.fileupload.MultipartStream $ ItemInputStream.makeAvailable(MultipartStream.java:982) 在 org.apache.commons.fileupload.MultipartStream $ ItemInputStream.read(MultipartStream.java:886) 在java.io.InputStream.read(未知来源)at org.apache.commons.fileupload.util.Streams.copy(Streams.java:96)at org.apache.commons.fileupload.util.Streams.copy(Streams.java:66)at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:366) ......还有26个