在java中发布带有multipart的视频

时间:2018-02-17 21:03:44

标签: java http post

我正在尝试使用请求Http Post将带有java的视频上传到Microsoft的api(Video Indexer API),并且它与Postman合作

但是当我在java中这样做时,它不起作用

这是我的代码:

public static void main(String[] args)
{
    CloseableHttpClient httpclient = HttpClients.createDefault();

    try
    {

        URIBuilder builder = new URIBuilder("https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns?name=film2&privacy=Public");

        URI uri = builder.build();


        HttpPost httpPost = new HttpPost(uri);

        httpPost.setHeader("Content-Type", "multipart/form-data");
        httpPost.setHeader("Ocp-Apim-Subscription-Key", "19b9d647b7e649b38ec9dbb472b6d668");

        MultipartEntityBuilder multipart = MultipartEntityBuilder.create();

        File f = new File("src/main/resources/film2.mov");
        multipart.addBinaryBody("film2",new FileInputStream(f));

        HttpEntity entityMultipart = multipart.build();

        httpPost.setEntity(entityMultipart);

        CloseableHttpResponse response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();

        if (entity != null)
        {
            System.out.println(EntityUtils.toString(entity));
        }
    }

这就是错误:

{"ErrorType":"INVALID_INPUT","Message":"Content is not multipart."}

对于Postman来说,这是我必须在Http请求上放置的所有参数的屏幕

Screen for all the params on postman

标题:

header

2 个答案:

答案 0 :(得分:0)

最后我发现了问题

是这一行:httpPost.setHeader(" Content-Type"," multipart / form-data");

如果我使用MultipartEntityBuilder,我不必写出Content-Type是多部分......

答案 1 :(得分:0)

尝试此解决方案肯定有效 当您将邮递员用于多部分请求时,请不要在页眉中指定自定义Content-Type。因此,您在Postman中的Header标签应该为空。邮递员将确定表单数据边界。在Postman的“正文”选项卡中,应该选择表单数据并选择文件类型。