SparkJava multipart/form-data fileUpload

Spark Framework is a simple and lightweight Java web framework built for rapid development. With Spark it’s possible to start a REST web server with a few lines of code.It is the Java porting of Sinatra: famous micro-framework written in Ruby.The purpose of this post is to explain how to work with Spark  for multipart/form-data fileUpload requirement.In different dev community I have seen people searching fix of fileUpload failure of SparkJava framework, so I thought to share the fix that I have encountered and fixed recently."/files/upload/:userName", "multipart/form-data", new Route() {
			public Object handle(Request request, Response response) {
				// process request
				String userID = request.params("userName");
				if (isValidUser(userID)) {
// These two lines work as fix.
MultipartConfigElement multipartConfigElement = new MultipartConfigElement("data/tmp");
request.raw().setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement);

					Collection<Part> parts = null;
					try {
						parts = request.raw().getParts();
					} catch (IOException | ServletException e2) {
						// TODO Auto-generated catch block
					for (Part part : parts) {
						System.out.println("Name:" + part.getName());
						System.out.println("Size: " + part.getSize());
						System.out.println("Filename:" + part.getSubmittedFileName());
					String fName = null;
					Part file = null;
					try {
						file = request.raw().getPart("fileToBeUploaded");
						fName = request.raw().getPart("fileToBeUploaded").getSubmittedFileName();
					} catch (IOException | ServletException e1) {



8 thoughts on “SparkJava multipart/form-data fileUpload

  1. I am trying to process both multipart zip file and also the json object from the request using SparkJava, but the request becomes empty once we use it parse the zip file and the same cannot be used to parse the json object.
    Is there any way to accomplish this using SparkJava


      • Hello SkilledMinds,

        The request has two parts, one is a zip file and other other being json object
        currently i am hitting from postman, from postman request will be like ‘form-data’ in ‘body’
        one is text and other is file
        text > data : {“assetName”:”arstest2″,”service”:”Blueprint POC”,”operationType”:”CREATE” ….
        file > fileStream : zip file

        I am processing the zip file in the below manner
        post(“/agent/blueprint/create”, (request, response) ->{

        String responseObject = null;
        if (request.raw().getAttribute(“org.eclipse.jetty.multipartConfig”) == null) {
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement(System.getProperty(“”));
        request.raw().setAttribute(“org.eclipse.jetty.multipartConfig”, multipartConfigElement);

        Part file = request.raw().getPart(“fileStream”);
        if (file.getContentType().startsWith(“application/octet-stream”)) {
        Part uploadedFile = request.raw().getPart(“fileStream”);
        InputStream inputStream = uploadedFile.getInputStream();
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ZipEntry entry = null;

        while ((entry = zipInputStream.getNextEntry()) != null) {

        Once the above processing is done fine, then if I try to get request body to process the json, the request has nothing in it
        I am not able to parse both zip file(fileStream) and json object(data) from the request.

        Thanks in advance


      • Ok got it. Can I post the solution by 14th May, actually I am very busy with an assignment. Friday eve I will be free then I will post the solution.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.