2014-10-09 4 views
1

내 안드로이드 응용 프로그램이 파일을 원격 PHP 서버에 성공적으로 보냅니다. 서버가 단순한 문자열을 다시 보내 응답을 알고 때까지Android : HttpResponse로 PHP 서버에서 파일 가져 오기

PHP 코드

// if a file was posted 
if($_FILES && $_POST){ 

    if(move_uploaded_file($file, $dir)) { 
     echo "File successfully sent to server."; 
    } 
나는 코드의 평화와이 텍스트 답변을 얻을 수 있습니다

:

자바 코드

HttpResponse response = httpClient.execute(httpPost); 
HttpEntity resEntity = response.getEntity(); 

// get server response 
if (resEntity != null) {  
    return EntityUtils.toString(resEntity).trim(); 
} 

이제 간단한 텍스트 응답을 받고 싶지는 않지만 전체 파일. 나는 많은 경험을 가지고 있지 않고 붙어있어 : 확실히 바이트의 파일 크기가 아닌

PHP 코드

if(move_uploaded_file($app, $upload_file . $file_ending)) { 
    readfile($res_file); // read file and write it output buffer 
} 

자바 코드

HttpResponse response = httpClient.execute(httpPost); 
HttpEntity resEntity = response.getEntity(); 

if (resEntity != null) 
    return String.valueOf(resEntity.getContentLength()); 

getContentLength 반환 14 .

파일을 PHP 서버에서 어떻게 응답합니까?

답변

1

보내는 파일이 14 바이트가 아닌지 확인하십시오. 오류 메시지가 아니라는 응답을 확인하십시오. 정당한 응답을받는 경우 response.getEntity(). getContentLength()에 "the number of bytes of the content"이 표시되어야합니다. PHP는 부분에 관해서는

, 여기에 뭔가 다른 사람이 성공적으로 사용하고 있습니다 :

if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
    exit; 
} 

reference를 참조하십시오.

+0

이것은 작동합니다. 고마워. 설명해주십시오. 왜이 모든 헤더 행을 추가해야합니까? 왜 나는 단지 readfile ($ file)을 실행할 수 없습니까? – null

+0

솔직히, 나는 당신이 그것을 필요로하지 않는다고 생각합니다. 디버깅에 도움이 될만한 것입니다. 내가 권하는 것은 파일을 보내기 전에 파일이 존재하는지 확인하는 것입니다. – erad

+1

이 줄이 없으면 같은 오류가 발생합니다. 따라서 그들은 필요합니다. – null