Java:从Multipart POST请求中获取视频文件

时间:2016-04-07 20:25:52

标签: java android rest google-app-engine jersey

我正在将一个视频文件从我的Android客户端发布到我的服务器作为Multipart请求。我需要编写一个服务器端方法来接收以下请求。

  • 我使用Jersey作为服务器端框架

我的代码如下:

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( );
    }

如何将 SERVER 端代码写入 RECEIVE 上述请求? 方法签名足以满足答案:)

1 个答案:

答案 0 :(得分:1)

究竟是什么问题? 不知道泽西岛,但步骤将是:

1)编写一个servlet(http://www.tutorialspoint.com/servlets/servlets-first-example.htm

2)Servlet输入参数HttpServletRequest包含getParts()方法,您可以在其中找到您发布的视频...以及其他部分(如果有的话)

修改

未经测试,但这对你有帮助吗?您应该能够像这样获取视频数据流。


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
        }
    }

有关详细示例,请参阅spring的用法: See spring way