Parse multipart / form-data using Apache Commons file upload

Does the Apache Commons File Upload package provide a common interface for the parse multipart/form-data chunks stream through an InputStream , adding an Array<Byte> or through any other common streaming interface?

I know that they have a streaming API, but the example shows you how to do this using ServletFileUpload , which, it seems to me, should be specific to Servlet .

If not, are there other alternative structures in the JVM that allow you to do just that? Unfortunately, the framework that I use, Spray.io, does not seem to make it possible to do this.

+5
source share
1 answer

bayou.io has a generic MultipartParser

You may need adapters to work with it, since it has its own Async and ByteSource interfaces.

The following example shows how to use a parser synchronously with an 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; } } 
+3
source

All Articles