【问题标题】:Android Unable to upload image to server. Wrong image typeAndroid 无法将图像上传到服务器。图片类型错误
【发布时间】:2014-09-04 20:56:03
【问题描述】:

我有一个使用 HttpCliend 和 MultipartEntityBuilder 将图像发送到服务器的应用程序。我的问题是发送不成功。我从服务器收到一条消息“无效文件”。

这是我的发送方式:

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

这就是我在 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

如您所见,文件获取到服务器但没有通过任何 if 条件。我尝试使用httpPostRequest.setHeader("Content-type", "image/png;");,但没有成功。你能告诉我问题是什么以及如何解决吗?谢谢。

【问题讨论】:

    标签: php android file-upload content-type multipartentity


    【解决方案1】:

    使用

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

    您想为文件设置内容类型,而不是为请求设置内容类型。 见org.apache.http documentation for more details

    【讨论】:

    • 不推荐使用 FileBody。
    • 您的意思是使用字符串作为第二个参数?那么推荐的方式似乎是new FileBody(file, new ContentType("image/png"));
    • 构造函数 ContentType(String) 未定义。我尝试了另一种方法 new FileBody(file, "image/png");但随后文件甚至没有到达服务器。
    • 查看文档。到 ContentType 的字符串看起来应该是 ContentType.create("image/png");
    • 那你删除 addTextBody() 了吗?
    【解决方案2】:

    我想通了。我将图像作为二进制正文上传,并在那里设置类型和名称:

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

    【讨论】:

      猜你喜欢
      • 2017-05-04
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 2014-03-21
      • 2014-03-24
      相关资源
      最近更新 更多