简体   繁体   English

用Java多部分发布视频

[英]Post a video with multipart in java

I am trying to upload a video with java to an api of Microsoft(Video Indexer API) using a request Http Post and it's work with Postman 我正在尝试使用请求Http Post将带有Java的视频上传到Microsoft(Video Indexer API)的api,并且可以与Postman一起使用

But when i do this in java it's not work 但是当我在Java中执行此操作时不起作用

This is my code : 这是我的代码:

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

And this is the error: 这是错误:

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

And for Postman this is a screen for all the parameter that i have to put on the Http Request 对于邮递员,这是我必须在Http请求中输入的所有参数的屏幕

Screen for all the params on postman 屏幕上邮递员的所有参数

And for the header: 对于标题:

header 标头

Finally I found the problem 终于我找到了问题

It was this line : httpPost.setHeader("Content-Type", "multipart/form-data"); 就是这一行:httpPost.setHeader(“ Content-Type”,“ multipart / form-data”);

I don't have to write that the Content-Type is multipart if I use a MultipartEntityBuilder ... 如果我使用MultipartEntityBuilder,我不必写Content-Type是多部分的...

Try this solution definitely works When you are using Postman for multipart request then don't specify a custom Content-Type in Header. 尝试此解决方案绝对有效当您使用邮递员进行多部分请求时,请不要在页眉中指定自定义Content-Type。 So your Header tab in Postman should be empty. 因此,您在Postman中的Header标签应该为空。 Postman will determine form-data boundary. 邮递员将确定表单数据边界。 In Body tab of Postman you should select form-data and select file type. 在邮递员的“正文”选项卡中,您应该选择表格数据并选择文件类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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