2014-11-11 2 views
0

Java에서 저에게 만든 webservice를 통해 비디오를 업로드하려고합니다. 때로는 동영상을 전송하지만 때로는 서버가 "400 오류, 매개 변수가 잘못되었습니다."동영상 업로드 안드로이드 서버 오류 404

저를 도와 줄 수 있습니까?

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

@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); 
} 
+0

많은 코드를 제공했지만 시도한 방법은 거의 제공하지 않았습니다. 좋은 질문은 응답자가 걸어 다닐 필요가있는 "코드 검토"의 양을 최소화하고 문제의 핵심에 도달하려고 시도하는 것입니다. 이 코드는 문제를 설명하는 가장 작은 코드입니까? – Kolban

+0

네 맞아. 난 그냥 내 코드를 단순화. 감사합니다 :) –

답변

0

문제가 서버 측에서 인 웹 서비스 자바. 봄에 MultipartFile 클래스를 사용해보십시오. 여기에 도움이되는 간단한 튜토리얼이 있습니다 http://www.journaldev.com/2573/spring-mvc-file-upload-example-tutorial-single-and-multiple-files

+0

하지만 내가 안드로이드 클라이언트에서 다른해야 무엇입니까? –

+0

SystemUtils.decodeFile (filePath)을 사용하여 비트 맵으로 변환하여 파일을 보낸 다음 ByteArrayOutputStream으로 변환하고 바이트 배열을 가져 와서이 entity.addPart (fileName, contentBody)와 같은 엔터티에 추가했습니다. –