0

我正在开发 android 应用程序,用户可以从中选择设备中的文件并将其上传到服务器。该应用程序在模拟器上运行良好,但是当我在真实设备上测试它时,我没有得到文件。

private void showFileChooser() {


    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    startActivityForResult(intent, PICKFILE_RESULT_CODE);

}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1:
            if (resultCode == RESULT_OK) {

                Uri uri = data.getData();
                String uriString = uri.toString();
                File myFile = new File(uriString);
                String path = myFile.getAbsolutePath();
                displayName = null;

                if (uriString.startsWith("content://")) {
                    Cursor cursor = null;
                    try {
                        cursor = this.getContentResolver().query(uri, null, null, null, null);
                        if (cursor != null && cursor.moveToFirst()) {
                            displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                        }
                    } finally {
                        cursor.close();
                    }
                } else if (uriString.startsWith("file://")) {
                    displayName = myFile.getName();

                    Log.i("tagconvertstr", "[" + displayName + "]");
                }
                // txt_file_name_1.setText(displayName);
            }

            txt_file_name_1.setText(displayName);
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}







public class PostDataAsyncTask extends AsyncTask<String, String, String> {


    @Override
    protected String doInBackground(String... strings) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://pitechnologiesindore.in/pitechno/newfile.php");

            String name = ettname.getText().toString();
            String number = ettnumber.getText().toString();
            String idea = ettidea.getText().toString();
            items = spinner2.getSelectedItem().toString();
            java.util.Date dt = new java.util.Date();
            java.text.SimpleDateFormat sdf =
                    new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String currentTime = sdf.format(dt);



            File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), displayName);
            FileBody fileBody1 = new FileBody(file1);


            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("file1", fileBody1);
            reqEntity.addPart("name", new StringBody(name));
            reqEntity.addPart("number", new StringBody(number));
            reqEntity.addPart("idea", new StringBody(idea));
            reqEntity.addPart("items", new StringBody(items));
            reqEntity.addPart("date", new StringBody(currentTime));
            httpPost.setEntity(reqEntity);

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null) {
                final String responseStr = EntityUtils.toString(resEntity)
                        .trim();


            }

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        Log.e("", "RESULT : " + result);

    }
}

我的 PHP 文件

  <?php

  define('HOST','localhost');
  define('USER','****');
  define('PASS','****');
  define('DB','pi1');
  $con = mysqli_connect(HOST,USER,PASS,DB);

$file_path = "resume/";
$file_path = $file_path . basename( $_FILES['file1']['name']);
$fname= $_FILES['file1']['name'];
if(move_uploaded_file($_FILES['file1']['tmp_name'], $file_path)) {

    echo "file saved success";


} else{

   echo "failed to save file";
}



  $name = $_POST['name'];
  $number = $_POST['number'];
  $idea = $_POST['idea'];
  $items = $_POST['items'];
  $date = $_POST['date'];
  $fname= $_FILES['file1']['name'];
$sql = "insert into career (name, age, skill, file_upload, message,date) values ('$name','$number','$items','$fname','$idea','$date')";
  if(mysqli_query($con,$sql)){
    echo 'success';
  }
  else{
    echo 'failure';
  }



?>
4

0 回答 0