I’m trying to create a lightweight, self-contained webservice using the spark microframework ( http://www.sparkjava.com/readme.html ). I need to work with multi-part forms (I want to receive both a file, and some key-value data).
Jetty (on which Spark depends) provides a MultiPartFilter filter which facilitates working with multipart data, but I don’t understand how to hook into that in my code.
I need to do this programmatically because this service will not be deployed as part of a giant java installation, but to support a python application.
The code I have is along these lines:
public class Transcoder {
static Base64 base64 = new Base64();
public static void main(String[] args) {
org.apache.log4j.BasicConfigurator.configure();
post(new Route("/convert") {
@Override
public Object handle(Request request, Response response) /*throws Exception, Docx4JException*/{
//I want to do something like this:
new_request = new MultiPartFilter().process_my_request(request);
/* work with altered request*/
});
}
}
Is this possible?
I don’t have the sources for Jetty (or Spark) loaded up, but I was just looking in the Spring sources and found an Interface called
MultipartResolver, which has a methodresolveMultipartwhich looks like it would do what you want/need. I would not be surprised to find classes implementing a similar named interface in Jetty:Note that Commons-FileUpload package also provides a nice set of utitilies to perform the same type of process that you want, without needing to re-work the Jetty Filter.