java - Android:将视频作为 POST 请求上传到 Java 后端?

标签 java android rest post jax-rs

我只需将视频从我的 Android 设备上传到我的 Java 后端,在阅读了一些 StackOverflow 线程后,我了解到我需要将我的视频作为多部分请求发布到 Java 后端。

我设法实现了以下内容,它基本上将视频文件作为多部分 POST 请求进行 POST。

Android 客户端:

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

我的问题是,如何从 Java 后端接收文件?这是我需要修改的后端方法。有人可以指出我如何从后端接收视频文件吗?

我现在拥有的:

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

最佳答案

这是我的做法(没有错误处理、验证等)。

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

关于java - Android:将视频作为 POST 请求上传到 Java 后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36458541/

相关文章:

javax.xml.ws.soap.SOAPFaultException : Unmarshalling Error

http - 如何通知 REST API 客户端进行可选/必需的更新?

android - 装载机 : onLoadFinished called only once

rest - 如果资源正在使用且无法删除,请更正 Delete API 的 HTTP 响应代码

rest - 如何使用 JAXRS 和 JAXB 设置 ReSTLet 服务器?

java - 快速将 int[] 写入磁盘?

java - 在名称为 'appServlet' 的 DispatcherServlet 中找不到具有 URI [/myappname/] 的 HTTP 请求的映射

java - 如何设置编译器选项,使其拒绝该程序,使其无法静态确保正确性?

java - 如何使用 Intent 传递类的对象?

Android - 打开名称中包含空白字符的文件时出现 FileNotFoundException