我有这种方法可以“处理分段上传”流:
public class FileUpload {
public static FileItemIterator parse(final byte[] data, final String contentType) {
try {
final ServletFileUpload upload = new ServletFileUpload();
final HttpServletRequest request = new MockHttpServletRequest(data, contentType);
final boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if ((!isMultipart)) {
throw new Exception("Illegal request for uploading files. Multipart request expected.");
}
FileItemIterator iter = upload.getItemIterator(request);
return iter;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
}
数据是InputStream
并转换为字节数组的地方:
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myFile"; filename="test.txt"
Content-Type: text/plain
Simple file.
-----------------------------8721656041911415653955004498--
contentType为multipart/form-data; boundary=-----------------------------8721656041911415653955004498
此代码的问题是FileIterator
始终为空。我认为这里的数据参数是正确的,并且基于MIME标准。 迭代器为空的原因可能是什么?
ps。这是一个独立的Uber Jar型程序,因此不是基于Servlet的