O upload do Android do PHP falhou

Atualmente, estou usando o hosting24.com para a implementação do upload de arquivos php e vai fazer o upload de arquivos mp4. O arquivo html para testes, funciona. Quando se trata da execução por deivces do Android, ele falha e mostra o $ name = $ _FILES [“uploaded_file”] é nulo. Você poderia me dizer se existem outros fatores que nos impedem de carregar com sucesso?

O abaixo é meu código

public String uploadFIle(File file){ String result = ""; HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; String fileName = file.getName(); int bytesRead, bytesAvailable, bufferSize; int serverResponseCode = 0; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; FileInputStream fileInputStream; try { fileInputStream = new FileInputStream(file); URL url = new URL("http://www.gallicalab.com/upload.php"); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) serverResponseCode = conn.getResponseCode(); InputStream is = conn.getInputStream(); if(serverResponseCode==200){ BufferedReader reader = new BufferedReader(new InputStreamReader( is, "utf-8"), 8192); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } result = sb.toString(); } // result = serverResponseMessage; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } 

Lado do servidor

 <?php error_reporting(E_ALL); $target_path = "target/"; // $target_path = $target_path . basename( $_FILES['file']['name']); $name = $_FILES["uploaded_file"]["name"]; // echo $name."
"; $ext = end(explode(".", $name)); // echo $ext."
"; $randname = random_string(30); // echo $randname."
"; $fname = $randname . "." .$ext; //echo $fname."
"; $target_path = $target_path .$fname; // echo $target_path."
"; if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { //echo "The file ". basename( $_FILES['file']['name'])." has been uploaded"; //echo random_string(50); // $arr = array ('data'=>'http://gallicalab.com/target/'.$_FILES['file']['name']); $arr = array ('data'=>'http://gallicalab.com/target/'.$fname); echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5} } else { echo "There was an error uploading the file, please try again!"; } function random_string($length) { $key = ''; $keys = array_merge(range(0, 9), range('a', 'z')); for ($i = 0; $i

Você pode registrar o que você realmente está recebendo do lado do servidor para um arquivo? O aplicativo Android talvez não esteja fazendo a solicitação corretamente.

Tente isso:

  

Em seguida, inspecione o arquivo upload.log e veja o que aconteceu com sua solicitação do dispositivo.


PS Experimente este código de upload alternativo para o Android:

 private void uploadFile(String filePath, String fileName) { InputStream inputStream; try { inputStream = new FileInputStream(new File(filePath)); byte[] data; try { data = IOUtils.toByteArray(inputStream); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://www.gallicalab.com/upload.php"); InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), fileName); MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart("file", inputStreamBody); httpPost.setEntity(multipartEntity); HttpResponse httpResponse = httpClient.execute(httpPost); // Handle response back from script. if(httpResponse != null) { } else { // Error, no response. } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } } 

No lado do servidor:

 $objFile = & $_FILES["file"]; $strPath = basename( $objFile["name"] ); if( move_uploaded_file( $objFile["tmp_name"], $strPath ) ) { print "The file " . $strPath . " has been uploaded."; } else { print "There was an error uploading the file, please try again!"; } 

Fonte: http://www.codepuppet.com/2013/03/26/android-uploading-a-file-to-a-php-server/