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

发布于 2025-01-06 15:40:05 字数 2674 浏览 3 评论 0原文

我必须使用 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}

I have to upload a 3gp audio file to my server using MultipartEntity. i am using this code but still unable to upload 3gp file.

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

Kindly suggest me where i am making mistake?

LogCat Response

    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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2025-01-13 15:40:05

试试这个。

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

让我知道这是否有效。

Try this out.

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

Let me know if this works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文