Apache Commons 文件上传包是否提供了一个通用接口(interface)来进行流解析 multipart/form-data
通过 InputStream
分块, 追加 Array<Byte>
,或通过任何其他通用流媒体接口(interface)?
我知道他们有一个流式 API,但该示例仅向您展示了如何通过 ServletFileUpload
执行此操作,我认为这必须特定于 Servlet
.
如果没有,JVM 中是否有任何其他替代框架可以让您完全做到这一点?遗憾的是,我使用的框架 Spray.io 似乎没有提供执行此操作的方法。
最佳答案
bayou.io有一个通用的 MultipartParser
你可能需要一些适配器来使用它,因为它有自己的 Async 和 ByteSource 接口(interface)。
以下示例展示了如何与 InputStream
同步使用解析器
String msg = ""
//+ "preamble\r\n"
+"--boundary\r\n"
+"Head1: Value1\r\n"
+"Head2: Value2\r\n"
+"\r\n"
+"body.body.body.body."
+"\r\n--boundary\r\n"
+"Head1: Value1\r\n"
+"Head2: Value2\r\n"
+"\r\n"
+"body.body.body.body."
+"\r\n--boundary--"
+ "epilogue";
InputStream is = new ByteArrayInputStream(msg.getBytes("ISO-8859-1"));
ByteSource byteSource = new InputStream2ByteSource(is, 1024);
MultipartParser parser = new MultipartParser(byteSource, "boundary");
while(true)
{
try
{
MultipartPart part = parser.getNextPart().sync(); // async -> sync
System.out.println("== part ==");
System.out.println(part.headers());
ByteSource body = part.body();
InputStream stream = new ByteSource2InputStream(body, Duration.ofSeconds(1));
drain(stream);
}
catch (End end) // control exception from getNextPart()
{
System.out.println("== end of parts ==");
break;
}
}
关于java - 使用 Apache Commons 文件上传解析 multipart/form-data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30387603/