1

我正在使用 multipartEntity 将视频上传到服务器。这是我的代码:

public boolean uploadVideoToStickyworld(String fileName, String title, String description, String iEndpointURI)
    {
        boolean retBool = false;

        try {

        HttpPost httpPost = new HttpPost(iEndpointURI);
        httpPost.setHeader("User-Agent", userAgent);
        httpPost.setHeader("Authorization", "Basic " + usnHeader);

        try {
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpPost httppost = new HttpPost(iEndpointURI);
        File file = new File(fileName);

        MultipartEntity mpEntity = new MultipartEntity();
        FileBody fileBody = new FileBody(file);
        mpEntity.addPart("title",new StringBody(title));
        mpEntity.addPart("description", new StringBody(description));
        mpEntity.addPart("video", fileBody);

        httppost.setEntity(mpEntity);
        System.out.println("executing request " + httppost.getRequestLine()+ "header "+ httppost.getURI());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        System.out.println(response.getStatusLine());

        if (resEntity != null) {
          resEntity.consumeContent();
          System.out.println(EntityUtils.toString(resEntity));
          retBool = true;
        }

        }catch (Exception e) {
        }

    } catch(Exception e){

    }
    return retBool;
}

代码来自How to upload a file using Java HttpClient library working with PHP

但是服务器的响应是错误的请求:400

因为服务器使用多部分编码?谁能告诉我这段代码有问题吗?太感谢了。

4

0 回答 0