简体   繁体   中英

Java: Get Video file from Multipart POST request

I am POSTing a video file from my Android client to my server as a Multipart request. I need to write a server side method to receive the following request.

  • I am using Jersey as the server side Framework

My code is as follows:

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

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://MY_SERVER_URL/videos/postvideo");

        FileBody filebodyVideo = new FileBody(new File(videoPath));
        StringBody title = new StringBody(titleBox.getText().toString());
        StringBody description = new StringBody(captionBox.getText().toString());

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

        // DEBUG
        System.out.println( "executing request " + httppost.getRequestLine( ) );
        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( );
    }

How would I write the SERVER side code to RECEIVE the above request? The Method Signature will be sufficient for the answer :)

What is the problem exactly? Dont know about Jersey, but the steps would be:

1) Write a servlet ( http://www.tutorialspoint.com/servlets/servlets-first-example.htm )

2) Servlet input parameter HttpServletRequest contains getParts() method, where you would find your posted video...along with other parts if any

EDIT

Untested, but will this help you? You should be able to get the stream of the video data like this.


protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        Collection parts = req.getParts();
        for (Part part : parts) {
            //... determine if its a file part from content disposition for example
            InputStream is = part.getInputStream();
            //...work with your input stream
        }
    }

For detailed example, see how spring does it: See spring way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM