0

I need to simply upload a video from my Android Device to my Java Backend and after reading through some StackOverflow threads, I learnt that I need to POST my video as a Multipart request to the Java Backend.

I managed to implement the following, which basically POSTs the video file as a Multipart POST request.

Android Client:

private void uploadVideo(String videoPath) throws ParseException, IOException {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("MY_SERVER_URL");

        FileBody filebodyVideo = new FileBody(new File(videoPath));
        StringBody title = new StringBody("Filename: " + videoPath);
        StringBody description = new StringBody("This is a description of the video");

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("video", filebodyVideo);
        reqEntity.addPart("title", title);
        reqEntity.addPart("description", description);
        httppost.setEntity(reqEntity);

        // DEBUG
        HttpResponse response = httpclient.execute( httppost );
        HttpEntity resEntity = response.getEntity( );

        // DEBUG
        System.out.println( response.getStatusLine( ) );
        if (resEntity != null) {
            System.out.println( EntityUtils.toString(resEntity) );
        } // end if

        if (resEntity != null) {
            resEntity.consumeContent( );
        } // end if

        httpclient.getConnectionManager( ).shutdown();
}

My question is, how do I receive the file from the Java Backend? Here's the Backend method that I need to modify. Can someone point out how I can receive the video file from the backend?

What I have right now:

@Path("/user")
public class UserAPI {

    @POST
    //To receive the file, What do I add below instead of the lines I've commented.
    //@Produces(MediaType.APPLICATION_JSON)
    //@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("/postvideo")
    public VideoResponse PostVideo(){
        //My code
    }
}
Jay
  • 4,873
  • 7
  • 72
  • 137

1 Answers1

1

Here is how I did it (without error handling, validation and stuff).

@POST
@Path("/")
@Consumes("multipart/form-data")
public Response uploadFileMultipart(MultipartFormDataInput input) {
        Map<String, List<InputPart>> uploadForm = input.getFormDataMap();

        List<InputPart> inputParts = uploadForm.get("video");

        String videoFileName = "GENERATE_YOUR_FILENAME_HERE.mp4";
        File file = new File(filename);

        if (!file.exists()) {
            file.createNewFile();
        }

        FileOutputStream fop = new FileOutputStream(file);
        for (InputPart inputPart : inputParts) {
                InputStream inputStream = inputPart.getBody(InputStream.class, null);
                byte[] content = IOUtils.toByteArray(inputStream);
        fop.write(content);
        }

        fop.flush();
        fop.close();

        return Response.status(HttpStatus.SC_OK).build();
}
  • So the Content-type would be 'multipart/form-data'? Is that correct? – Jay Apr 06 '16 at 18:59
  • I have some import issues. I can't seem to find an import for 'InputPart' and 'MultipartFormDataInput'. Should I add a JAR? – Jay Apr 06 '16 at 19:02
  • What framework are you using? Maybe you could add this to your question, the code in my answer was written with RESTEasy – Sven Pfeiffer Apr 06 '16 at 19:09
  • The content-type needs to fit what you are sending, definitely some form of multipart/ – Sven Pfeiffer Apr 06 '16 at 19:10
  • I am using JAX-RS, Sven. Do you recommend RESTEasy? – Jay Apr 06 '16 at 19:11
  • If I am not wrong JAX-RS is just the specification, RESTEasy is one of several implementations of the spec. If I had to guess I would say you are using jersey, since it is the reference-implementation. I do like RESTEasy and have been using it for yours, so I would recommend it. But just based on because I know how to use it, not because it is better than another implementation :) – Sven Pfeiffer Apr 06 '16 at 19:17
  • Thank you for the detailed answer and yes it is Jersey, my mistake. Do you know how to implement your answer in Jersey? :) – Jay Apr 06 '16 at 19:28
  • http://stackoverflow.com/questions/27609569/file-upload-along-with-other-object-in-jersey-restful-web-service should help for Jersey :) – Sven Pfeiffer Apr 06 '16 at 19:35