简体   繁体   English

使用Apache Commons File Upload解析multipart / form-data

[英]Parsing multipart/form-data using Apache Commons File Upload

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? Apache Commons File Upload包是否提供通用接口来通过InputStream流式解析multipart/form-data块,附加Array<Byte> ,还是通过任何其他通用流接口?

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 . 我知道他们有一个流API,但这个例子只告诉你如何通过ServletFileUpload来做到这ServletFileUpload ,我认为必须特定于Servlet

If not, are there any other alternative frameworks in JVM that lets you do exactly this? 如果没有,JVM中是否有任何其他替代框架可以让您完成此操作? Sadly, the framework that I am using, Spray.io, doesn't seem to provide a way to do this. 可悲的是,我使用的框架Spray.io似乎没有提供这样做的方法。

bayou.io has a generic MultipartParser bayou.io有一个通用的MultipartParser

You might need some adapters to work with it, since it has its own Async and ByteSource interfaces. 您可能需要一些适配器才能使用它,因为它有自己的AsyncByteSource接口。

The following example shows how to use the parser synchronously with InputStream 以下示例显示如何与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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM