2014-07-14 2 views
1

HttpCliend 및 MultipartEntityBuilder를 사용하여 이미지를 서버로 보내는 앱이 있습니다. 내 문제는 전송이 성공적이지 않다는 것입니다. 서버에서 "잘못된 파일"이라는 메시지가 나타납니다.Android 이미지를 서버에 업로드 할 수 없습니다. 잘못된 이미지 유형

이 내 보내는 방법 :

public void executeMultipartPost() throws ClientProtocolException, 
     IOException { 
    String result = ""; 
    Log.d("DrawingFragment", "executeMultipartPost"); 

    httpClient.setCookieStore(signiture.getCookies()); 

    HttpPost httpPostRequest = new HttpPost(url); 

    File file = new File(signiturePath); 

    FileBody bin = new FileBody(file); 
    Log.v("FileBody", bin.getFilename()); 
    Log.d("signiturePath", signiturePath); 
    MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder 
      .create(); 

    Log.i("HttpRequest Headers", "==========================================================="); 
    Header headers[] = httpPostRequest.getAllHeaders(); 
    for(Header h:headers) { 
     Log.i("", h.getName() + ": " + h.getValue() + "\n"); 
    } 
    Log.i("", "==========================================================="); 
    multiPartEntityBuilder.addTextBody("Content-Type", "image/png"); 

    multiPartEntityBuilder.addPart("file", bin); 

    httpPostRequest.setEntity(multiPartEntityBuilder.build()); 

    HttpResponse httpResponse = null; 
    httpResponse = httpClient.execute(httpPostRequest); 

    Log.i("HttpResponse Headers", "==========================================================="); 
    headers = httpResponse.getAllHeaders(); 
    for(Header h:headers) { 
     Log.i("", h.getName() + ": " + h.getValue() + "\n"); 
    } 
    Log.i("", "==========================================================="); 

    InputStream inputStream = null; 
    inputStream = httpResponse.getEntity().getContent(); 

    if (inputStream != null) { 
     result = convertInputStreamToString(inputStream); 
    } else { 
     result = "Did not work!"; 
    } 

    Log.i("UploadPhoto", result); 
} 

그리고 이것은 PHP 서버입니다 :

<?php 
$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$temp = explode(".", $_FILES["file"]["name"]); 
$extension = end($temp); 

echo "Upload: " . $_FILES["file"]["name"] . "\n"; 
echo "Upload: " . $_FILES["file"]["type"] . "\n"; 

if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/jpg") 
|| ($_FILES["file"]["type"] == "image/pjpeg") 
|| ($_FILES["file"]["type"] == "image/x-png") 
|| ($_FILES["file"]["type"] == "image/png")) 
&& in_array($extension, $allowedExts)) { 
    echo "Passed First If"; 
    if ($_FILES["file"]["error"] > 0) { 
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
    } else { 
    echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 
    if (file_exists("upload/" . $_FILES["file"]["name"])) { 
     echo $_FILES["file"]["name"] . " already exists. "; 
    } else { 
     move_uploaded_file($_FILES["file"]["tmp_name"], 
     "./" . $_FILES["file"]["name"]); 
     echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
    } 
    } 
} else { 
    echo "Invalid file"; 
} 
?> 

그리고 이것은 내가 로그 캣에있는 것입니다 : 당신으로

07-14 22:57:05.720: I/HttpRequest Headers(14081): =========================================================== 
07-14 22:57:05.720: I/(14081): =========================================================== 
07-14 22:57:05.960: I/HttpResponse Headers(14081): =========================================================== 
07-14 22:57:05.960: I/(14081): Date: Mon, 14 Jul 2014 20:59:38 GMT 
07-14 22:57:05.960: I/(14081): Server: Microsoft-IIS/6.0 
07-14 22:57:05.960: I/(14081): X-Powered-By: ASP.NET 
07-14 22:57:05.960: I/(14081): X-Powered-By: PHP/5.3.5 
07-14 22:57:05.960: I/(14081): Content-type: text/html 
07-14 22:57:05.960: I/(14081): Content-Length: 63 
07-14 22:57:05.960: I/(14081): =========================================================== 
07-14 22:57:05.960: I/UploadPhoto(14081): Upload: image.pngUpload: application/octet-streamInvalid file 

파일 get을 서버로 볼 수 있지만 if 조건은 통과하지 않습니다. httpPostRequest.setHeader("Content-type", "image/png;");으로 시도했지만 작동하지 않았습니다. 문제가 무엇인지 어떻게 고칠 수 있는지 말해 줄 수 있습니까? 감사.

답변

0

알아 냈습니다. 이진 본문으로 이미지를 업로드하고 형식과 이름을 설정합니다 :

multiPartEntityBuilder.addBinaryBody(imageName, byteArray, ContentType.create("image/png"), "image.png"); 
1

당신은없는 요청에 대한 파일의 콘텐츠 유형을 설정하려는

FileBody bin = new FileBody(file, "image/png"); 

사용. org.apache.http documentation for more details

+0

FileBody는 더 이상 사용되지 않습니다. – definera

+0

문자열을 두 번째 인수로 사용한다는 의미입니까? 권장되는 방법은'새로운 FileBody (file, new ContentType ("image/png")); '입니다. –

+0

생성자 ContentType (String)은 정의되지 않습니다. 나는 새로운 FileBody (file, "image/png")로 다른 방법을 시도했다. 하지만 파일은 서버에 도착하지 않습니다. – definera