2012-08-24 79 views
0

這裏是我的代碼上傳文件錯誤大

private Boolean doFileUpload() { 
     HttpEntity resEntity; 
     String urlString = "http://192.168.1.112/johnson/learn/android/index2.php"; 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(urlString); 
      FileBody bin1 = new FileBody(file1); 
      FileBody bin2 = new FileBody(file2); 
      MultipartEntity reqEntity = new MultipartEntity(); 
      reqEntity.addPart("uploadedfile1", bin1); 
      reqEntity.addPart("uploadedfile2", bin2); 
      reqEntity.addPart("user", new StringBody("User")); 
      post.setEntity(reqEntity); 
      HttpResponse response = client.execute(post); 
      resEntity = response.getEntity(); 
      final String response_str = EntityUtils.toString(resEntity); 
      if (resEntity != null) { 
       Log.i("RESPONSE", response_str); 
       runOnUiThread(new Runnable() { 
        public void run() { 
         try { 
          showToast("Upload Complete. Check the server uploads directory."); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } 
       }); 
      } 
     } catch (Exception ex) { 
      Log.e("Debug", "error: " + ex.getMessage(), ex); 
     } 
     return true; 
    } 

當我上傳2MB左右的文件它的工作原理,但錯誤,當我上傳20個+ MB的文件 任何人都知道的錯誤? (錯誤=數據沒有上傳,但指出上傳)

它指出上傳成功爲兩者。

我logcat的

08-24 13:27:15.725: D/dalvikvm(4221): GC_CONCURRENT freed 324K, 5% free 9538K/9991K, paused 0ms+0ms 
08-24 13:27:18.417: I/RESPONSE(4221): <br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <b>Notice</b>: Undefined index: uploadedfile1 in <b>C:\xampp\htdocs\Johnson\Learn\android\index2.php</b> on line <b>6</b><br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <b>Notice</b>: Undefined index: uploadedfile1 in <b>C:\xampp\htdocs\Johnson\Learn\android\index2.php</b> on line <b>7</b><br /> 
08-24 13:27:18.417: I/RESPONSE(4221): There was an error uploading the file, please try again!<br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <b>Notice</b>: Undefined index: uploadedfile1 in <b>C:\xampp\htdocs\Johnson\Learn\android\index2.php</b> on line <b>12</b><br /> 
08-24 13:27:18.417: I/RESPONSE(4221): filename: target_path: uploads/<br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <b>Notice</b>: Undefined index: uploadedfile2 in <b>C:\xampp\htdocs\Johnson\Learn\android\index2.php</b> on line <b>16</b><br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <b>Notice</b>: Undefined index: uploadedfile2 in <b>C:\xampp\htdocs\Johnson\Learn\android\index2.php</b> on line <b>17</b><br /> 
08-24 13:27:18.417: I/RESPONSE(4221): There was an error uploading the file, please try again!<br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <b>Notice</b>: Undefined index: uploadedfile2 in <b>C:\xampp\htdocs\Johnson\Learn\android\index2.php</b> on line <b>22</b><br /> 
08-24 13:27:18.417: I/RESPONSE(4221): filename: target_path2: uploads/<br /> 
08-24 13:27:18.417: I/RESPONSE(4221): <b>Notice</b>: Undefined index: user in <b>C:\xampp\htdocs\Johnson\Learn\android\index2.php</b> on line <b>26</b><br /> 
08-24 13:27:18.417: I/RESPONSE(4221): n String Parameter send from client side : 

這是我的PHP腳本

<?php 
$target_path1 = "uploads/"; 
$target_path2 = "uploads/"; 
/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */ 
$target_path1 = $target_path1 . basename($_FILES['uploadedfile1']['name']); 
if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1)) { 
    echo "The first file ". basename($_FILES['uploadedfile1']['name']). 
    " has been uploaded."; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
    echo "filename: " . basename($_FILES['uploadedfile1']['name']); 
    echo "target_path: " .$target_path1; 
} 

$target_path2 = $target_path2 . basename($_FILES['uploadedfile2']['name']); 
if(move_uploaded_file($_FILES['uploadedfile2']['tmp_name'], $target_path2)) { 
    echo "n The second file ". basename($_FILES['uploadedfile2']['name']). 
    " has been uploaded."; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
    echo "filename: " . basename($_FILES['uploadedfile2']['name']); 
    echo "target_path2: " .$target_path2; 
} 

$user = $_REQUEST['user']; 
echo "n String Parameter send from client side : " . $user; 
?> 
+0

究竟是什麼錯誤? – Eugene

+0

對不起,請查看我編輯的 – Sieryuu

+0

那是什麼printStackTrace給你?我懷疑 – Eugene

回答

1

您的文件是最有可能確實是在這兩種情況下正確上傳。但目標網址是一個PHP腳本。 PHP有一個限制(可以在PHP選項中設置)在帖子中接受多少數據(不記得默認值,但是很低)。

調查腳本可以多少數據接受。

+0

請參閱我的PHP腳本,必須在其中我補充? – Sieryuu

+1

如果您有權訪問'php.ini',則可以在其中更改值。例如'的upload_max_filesize = 10M'。否則,您可以在PHP腳本開始時更改我。 'ini_set('upload_max_filesize',100000);' – devnate