我有一個應用程序使用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";
}
?>
這是我在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
正如你可以看到get到服務器的文件,但沒有通過任何if條件。我試過httpPostRequest.setHeader("Content-type", "image/png;");
,但沒有奏效。你能告訴我什麼是問題,以及如何解決它?謝謝。
像這樣使用FileBody已被棄用。 – definera
你的意思是使用一個字符串作爲第二個參數?推薦的方式似乎是'新的FileBody(文件,新的ContentType(「圖像/ PNG」));'然後。 –
構造函數ContentType(String)未定義。我用另一種方式用新的FileBody(文件,「image/png」);但是這個文件甚至沒有到達服務器。 – definera