如何在android中使用MultiPartEntity在服务器上传3gp文件?

时间:2012-02-18 13:11:37

标签: android file file-upload http-post send

我必须使用MultipartEntity将3gp音频文件上传到我的服务器。我正在使用此代码但仍无法上传3gp文件。

FileInputStream is = new FileInputStream(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+
                                                                                    "/" + nameOfFile));
    byte[] data = IOUtils.toByteArray(is);

                    InputStreamBody isb = new InputStreamBody(new
                    ByteArrayInputStream(data),nameOfFile);
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://www.****.com/places/uploadVideo");
                    httppost.setHeader("Content-Type", "audio/3gpp");
                    MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                        multipartContent.addPart("video", isb);
                        Log.d("Audio length", isb.getFilename()+" "+isb.getMediaType()+ isb.getMimeType());
                        httppost.setEntity(multipartContent);

                        HttpResponse res =httpclient.execute(httppost);
                        InputStream input = res.getEntity().getContent();
                          BufferedReader rd = new BufferedReader(new InputStreamReader(input,"UTF-8"),input.toString().length());
                          String line;
                          StringBuilder sb =  new StringBuilder();
                          while ((line = rd.readLine()) != null) {
                                sb.append(line);
                          }
                          rd.close();
                        String  uploadresponse = sb.toString();

                        Log.d("Upload Pic Response",uploadresponse );

请建议我在哪里犯错?

LogCat响应

    02-18 19:58:51.912: D/Audio length(3157): 1329575327359.3gp applicationapplication/octet-stream
02-18 19:58:52.412: D/dalvikvm(3157): GC_FOR_MALLOC freed 2359 objects / 161592 bytes in 56ms
02-18 19:58:54.002: I/Resources(3157): Loaded time zone names for en_US in 200ms.
02-18 19:58:54.012: D/Upload Pic Response(3157): <strong>app/controllers/places_controller.php</strong> (line <strong>425</strong>)<pre class="cake-debug">Array(    [controller] => places    [action] => uploadVideo    [named] => Array        (        )    [pass] => Array        (        )    [plugin] =>     [form] => Array        (        )    [url] => Array        (            [url] => places/uploadVideo        ))</pre>{"response":{"status":0,"mesg":"No Video data present in the request"},"data":null}

1 个答案:

答案 0 :(得分:2)

试一试。

File f1 = null;
ContentBody cbFile, cbFile1;
// videofilename is the entire path file for eg. /mnt/sdcard/12345abc.3gpp
f1 = new File(videofilename);


cbFile = new FileBody(f1, "video/3gpp");
mpEntity.addPart("uservideo", cbFile);

让我知道这是否有效。