duanjian3920 2014-07-14 20:08
浏览 69
已采纳

Android无法将图像上传到服务器。 图像类型错误

I have an app that sends an image to a server using HttpCliend and MultipartEntityBuilder. My problem is that the sending isn't successful. I get a message "Invalid file" from the server.

This is my sending method:

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() + "
");
    }
    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() + "
");
    }
    Log.i("", "===========================================================");

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

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

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

And this is the php server:

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

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

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

And this is what I have in the logcat:

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

As you can see the file get's to the server but does not pass any of the if conditions. I tried with httpPostRequest.setHeader("Content-type", "image/png;"); but it didn't work. Can you tell me what is the problem and how to fix it? Thanks.

  • 写回答

2条回答 默认 最新

  • 普通网友 2014-07-16 13:08
    关注

    I figured it out. I upload the image as a binary body and there I set the type and the name:

    multiPartEntityBuilder.addBinaryBody(imageName, byteArray, ContentType.create("image/png"), "image.png");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿