【问题标题】:Upload Video Android server error 404上传视频 Android 服务器错误 404
【发布时间】:2015-01-08 04:05:23
【问题描述】:

我正在尝试通过我用 Java 创建的网络服务上传视频。有时也发送视频,但有时服务器返回“400错误,参数错误”

你们能帮帮我吗?

    private class SendVideoTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        try {

            final String url = server_url + "/rest/cloud/uploadFilePost";
            Form form = new Form();
            form.add("test_id", test_id);
            form.add("report_id", report_id);

            String encoded_file = cc.encodeFileToBase64Binary(mediaFile);
            form.add("encoded_file", encoded_file);
            String resp = "";
            CloseableHttpClient httpclient = HttpClients.createSystem();

            HttpPost httppost = new HttpPost(url);

            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();

            urlParameters.add(new BasicNameValuePair("test_id", test_id));
            urlParameters.add(new BasicNameValuePair("report_id", String
                    .valueOf(report_id)));
            urlParameters.add(new BasicNameValuePair("encoded_file",
                    encoded_file));

            httppost.setEntity(new UrlEncodedFormEntity(urlParameters));

            CloseableHttpResponse response = httpclient.execute(httppost);

            try {

                HttpEntity entity = response.getEntity();

                if (entity != null) {

                    InputStream instream = entity.getContent();
                    try {

                        resp = EntityUtils.toString(entity);

                    } finally {
                        instream.close();
                    }

                }

            } finally {
                response.close();
                httpclient.getConnectionManager().shutdown();
            }

            return resp;

        } catch (Exception e) {
            Log.e("VideoAsyncTask (Background)", e.getMessage(), e);
            // Toast.makeText(getApplicationContext(),
            // "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }

        return null;
    }
}

webservice java

@Produces(MediaType.TEXT_PLAIN)
@RequestMapping(value = "/rest/cloud/uploadFilePost", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> uploadPostVideo(
        @RequestParam(value = "test_id") String test_id,
        @RequestParam(value = "report_id") String report_id,
        @RequestParam(value = "encoded_file") String encoded_file) {

    String output = "";

    HttpHeaders responseHeaders = new HttpHeaders();

    Logger log = Logger.getLogger("com.vodafone.webmobiletestingsuite");

    byte[] bytes = Base64.decodeBase64(encoded_file);

    String file_name = "Video" + report_id + ".mp4";

    try {

        File file = new File(file_name);

        FileOutputStream fos = new FileOutputStream(file);

        fos.write(bytes);

        fos.close();

        log.info("Starting post cloud");

        cloudbo = new CloudBO();

        output = cloudbo.uploadFile(report_id, file);

        log.info("FILE PATH: " + file.getAbsolutePath());

        boolean r = file.delete();

        log.info("DELETE: " + r);

        log.info("Finish post cloud");

        UsabilityReport ur = boUsabilityReports.findById(
                Integer.parseInt(report_id), UsabilityReport.class);

        log.info("Start attachment");

        log.info("REPORT ID:" + ur.getExecutionReportId());

        CameraAttachment ca = new CameraAttachment();

        ca.setPublicURL(output.split(";")[1]);

        log.info("PUBLIC URL:" + output.split(";")[1]);

        ca.setUsability_report(ur);

        log.info("Finish attachment");

        boCameraAttachments.create(ca);

    } catch (FileNotFoundException e) {

        output = "filenotfound_exception";

    } catch (IOException e) {

        output = "io_exception";

    } catch (DbxException e) {

        output = "Dbx: " + e.toString();

    }

    responseHeaders.set("Access-Control-Allow-Origin", "*");
    responseHeaders.set("Access-Control-Allow-Methods",
            "POST, GET, PUT, UPDATE, OPTIONS");
    responseHeaders.set("Access-Control-Allow-Headers",
            "Content-Type, Accept, X-Requested-With");

    return new ResponseEntity<String>(output, responseHeaders,
            HttpStatus.OK);
}

【问题讨论】:

  • 您提供了很多代码,而您尝试的方式却很少。一个好的问题试图尽量减少响应者需要进行的“代码审查”,并试图触及问题的核心。这真的是说明问题的最小代码吗?
  • 是的,你说得对。我只是简化我的代码。谢谢:)

标签: android web-services post video upload


【解决方案1】:

问题出在您的服务器端。尝试在春季使用 MultipartFile 类。这是一个简单的教程,可以帮助http://www.journaldev.com/2573/spring-mvc-file-upload-example-tutorial-single-and-multiple-files

【讨论】:

  • 但是我必须在 android 客户端中做些什么不同?
  • 我通过使用 SystemUtils.decodeFile(filePath) 转换为位图来发送文件,然后我转换为 ByteArrayOutputStream,得到字节数组并像这个 entity.addPart(fileName, contentBody) 一样附加到我的实体
猜你喜欢
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 2011-06-28
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
  • 2012-02-07
相关资源
最近更新 更多