2014-11-04 3 views
0

HttpClient를 사용하여 미디어 파일을 서버에 게시하려고합니다. 내 코드는 이미지 파일에 잘 적용되지만 비디오 파일 (mp4)은 재생할 수 없습니다. 파일을 게시 내 코드 :HttpClient를 사용하여 Base64로 인코딩 된 비디오 파일 올리기

HttpClient httpclient = new DefaultHttpClient(); 
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

    HttpPost httppost = new HttpPost(REMOTE + "/add_file.php"); 

    MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create(); 
    ContentBody cbFile = null; 
    String mimeType = ""; 
    if (file.getName().endsWith(".jpg") || file.getName().endsWith(".jpeg")) { 
     mimeType = "image/jpeg"; 
    } else if (file.getName().endsWith(".mp4")) { 
     mimeType = "video/mp4"; 
    } 


    mpEntity.addTextBody("recipient_phone", recipientPhoneStr); 
    mpEntity.addTextBody("sender_phone", "55000"); 
    mpEntity.addTextBody("sender_key", "my_secret"); 
    mpEntity.addTextBody("file_name", file.getName()); 

    mpEntity.addTextBody("userfile", encodeFileToBase64Binary(file)); 

    httppost.setEntity(mpEntity.build()); 

    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity resEntity = response.getEntity(); 


    if (response.getStatusLine().toString().compareTo(HTTP_ERROR) == 0) { 
     throw new IOException(HTTP_ERROR); 
    } 

    if (resEntity != null) { 
     System.out.println(EntityUtils.toString(resEntity)); 
    } 
    if (resEntity != null) { 
     resEntity.consumeContent(); 
    } 

    httpclient.getConnectionManager().shutdown(); 

파일은 Base64.encodeBase64String (바이트)를 사용하여 인코딩 Base64로이다;

+0

모든 바이트를 올바르게 수신했는지 확인 했습니까? PHP의 post_max_size 또는 apache/nginx 제한을 사용 중일 수 있습니다. – bart

+0

감사합니다 바트. 예, post_max_size로 충분합니다 ... 어떻게 apache 한도를 확인합니까? – JB2

+0

파일을 바이트로 맵핑하고 POST의 'byteArrayEntity'에 버퍼를 랩핑하십시오. –

답변

1

https://hc.apache.org/httpcomponents-client-4.3.x/examples.html

체크 아웃 샘플 POST 프로그램 ...

바이트에 MP4 매핑 후는 POST를 실행하기위한 적합한 '엔티티 유형을 포장 다음 사용 ..

  FileInputStream fis = new FileInputStream(mfile); 
      FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory 
      int sz = (int)fc.size(); 
      MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); 
      byte[] data2 = new byte[bb.remaining()]; 
      bb.get(data2); 
      ByteArrayEntityHC4 reqEntity = new ByteArrayEntityHC4(data2); 
      httpPost.setEntity(reqEntity); 
      fis.close(); 

그런 다음 POST 유형의 요청에 대해 exec를 호출하십시오.

관련 문제