3

我想在多部分截击请求中上传视频。只想知道,如何使用以及如何在其中添加多部分参数。?

4

1 回答 1

0

您可以尝试这是一个通过 multapart 将视频或大文件发送到服务器的完整工作代码。

public static Boolean SendPostToServer(FBPost postData, Context context, String videoPath) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(context.getString(R.string.url_service_post));
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        if(!videoPath.isEmpty()){

            FileBody filebodyVideo = new FileBody(new File(videoPath));
            reqEntity.addPart("uploaded", filebodyVideo);
        }
        reqEntity.addPart("userId", new StringBody(postData.userId));
        reqEntity.addPart("postText", new StringBody(postData.postText));
        if(postData.postId != null && postData.postId.length() > 0) {
            reqEntity.addPart("postId", new StringBody(postData.postId));
        }
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }

        Log.e("Response: ", s.toString());
        return true;

    } catch (Exception e) {
        Log.e(e.getClass().getName(), e.getMessage());
        return false;
    }
}
于 2013-08-19T05:49:58.807 回答