JTK
JTK

Reputation: 1519

Parsing Multipart request in Play framework

I'm sending a video file and some other data from my Android application to my server running a Play framework application, I'm using a combination of multipartEntity and Volley to send the Post request to the server.

I need to parse the received multipartEntity request in my Controller and store the video in a folder on the server and send the rest of the data to a mySQL DB.

MultipartRequest:

private static final String FILE_PART_NAME = "fileKey";

public MultipartRequest(String url, File file, Map<String, String> mStringPart,
                                Response.Listener<String> listener,
                                Response.ErrorListener errorListener)
   super(Method.POST, url, errorListener);

        mListener = listener;
        mFilePart = file;
        this.mStringPart = mStringPart;
        entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        buildMultipartEntity();
    }


private void buildMultipartEntity() {
        entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
        for (Map.Entry<String, String> entry : mStringPart.entrySet()) {
            entity.addTextBody(entry.getKey(), entry.getValue());
        }
    }

@Override
public byte[] getBody() throws AuthFailureError {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            httpentity = entity.build();
            httpentity.writeTo(bos);
        } catch (IOException e) {
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        }
        return bos.toByteArray();
    }

This is my Controller so far:

public static Result uploadFile(){

        RequestBody body = request().body();
        Http.MultipartFormData multippartBody = body.asMultipartFormData();

        if(multippartBody != null) {
            return ok("Got: " + "Hi");
        } 
        else {
            return badRequest("Expecting text/plain request body");
        }
    }

I think I need some sort of iterator to to pull the video and other data from the multippartBody, but I'm unsure how to accomplish that

Edit

Updated Controller:

public static Result jsonPost(){

        RequestBody body = request().body();
        Http.MultipartFormData multippartBody = body.asMultipartFormData();
        Http.MultipartFormData.FilePart imageFile = body.getFile(fileKey);
        if(multippartBody != null) {
            return ok("Got: " + "hi");
        } 
        else {
            return badRequest("Expecting text/plain request body");
        }
    }
}

I'm getting a cannot find symbol error from Play on the fileKey?

Edit Got it working

public static Result jsonPost(){

        RequestBody body = request().body();
        Http.MultipartFormData multippartBody = body.asMultipartFormData();
        Http.MultipartFormData.FilePart imageFile = multippartBody.getFile("fileKey");

            if(imageFile.getFile() != null) {
                File file = imageFile.getFile(); 
                String fileName = imageFile.getFilename();
                File newDir = new File("/public/images/","MyApp");
                if(!newDir.isDirectory()){
                    newDir.mkdirs();
                    System.out.println("Created dir");
                }
                if(newDir.canWrite()){
                    file.renameTo(new File(newDir, fileName));
                }
                return ok("Got: " + "it");
            } 
            else {
                return badRequest("Expecting text/plain request body");
        }
    }

Upvotes: 2

Views: 2667

Answers (1)

Indrajeet
Indrajeet

Reputation: 647

Check for content-type in request headers, if it is application/octet-stream use

File file = request().body().asRaw().asFile();

if it is of type multipart/form-data use

play.mvc.Http.MultipartFormData body = request().body().asMultipartFormData();

play.mvc.Http.MultipartFormData.FilePart imageFile = body.getFile(imageKey);

File file = imageFile.getFile();

Upvotes: 4

Related Questions