Android使用multipartEntity将视频上传到服务器

时间:2014-02-26 17:20:37

标签: android upload multipartentity

我正在使用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

因为服务器使用多部分编码?谁能告诉我这段代码有什么问题?非常感谢你。

0 个答案:

没有答案