使用MultipartEntity上傳單張圖片。用了下面的代碼,但是圖片沒有上傳,也沒有任何錯誤提示。
我將所有的數據庫都加載了。
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://192.168.1.6/uploadimg.php");
httpClient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
File f = null;
FileBody fo = null;
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
// code for send image using post method
f = new File("/mnt/sdcard/a.png");
fo = new FileBody(f);
reqEntity.addPart("uploaded", fo);
Log.i("uploaded", "image added Parameter added");
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
Log.v("Upload photo", "Response" + s);
// return getUploadResponce(s.toString());
// Log.i("Response ", );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PHP 文件:
<?php
$file = $_FILES['uploaded']
move_uploaded_file($_FILES['uploaded']['tmp_name'], $_FILES['uploaded']['name']);
?>
看看服務器的臨時目錄的權限,ls -l 你的服務器文件移動到的目錄,php默認是root的權限,所以可以看下臨時目錄中是否有上傳的文件,如果有,說明是移動文件到目標目錄的權限問題;如果臨時目錄都沒有文件,說明是上傳代碼的問題。