使用Apache Commons File Upload解析多部分/表单数据 [英] Parsing multipart/form-data using Apache Commons File Upload
问题描述
Apache Commons File Upload软件包是否提供通用接口通过InputStream
,附加Array<Byte>
或任何其他通用流接口来流解析multipart/form-data
块?
Does Apache Commons File Upload package provides a generic interface to stream parse multipart/form-data
chunks via InputStream
, appending Array<Byte>
, or via any other generic streaming interface?
我知道他们有一个流API,但是该示例仅向您展示了如何通过ServletFileUpload
做到这一点,我认为这必须特定于Servlet
.
I know they have a streaming API but the example only shows you how to do that via ServletFileUpload
, which I reckon must be specific to Servlet
.
如果没有,那么JVM中是否有其他替代框架可以让您完全做到这一点?可悲的是,我正在使用的框架Spray.io似乎没有提供实现此目的的方法.
If not, are there any other alternative frameworks in JVM that lets you do exactly this? Sadly, the framework that I am using, Spray.io, doesn't seem to provide a way to do this.
推荐答案
bayou.io 具有通用的 MultipartParser
您可能需要一些适配器才能使用它,因为它有自己的适配器 异步 和 ByteSource 接口.
You might need some adapters to work with it, since it has its own Async and ByteSource interfaces.
以下示例显示如何与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;
}
}
这篇关于使用Apache Commons File Upload解析多部分/表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!