将文档、pdf、xls等从android应用程序上传到php服务器

将文档、pdf、xls等从android应用程序上传到php服务器,php,android,pdf,file-upload,Php,Android,Pdf,File Upload,我被困在那个地方,无法将文档文件发送到php服务器。 我正在使用这个代码 下面是PHP代码 下面是Java代码 private void showFileChooser() { Intent intent = new Intent(); intent.setType("file/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser

我被困在那个地方,无法将文档文件发送到php服务器。 我正在使用这个代码

下面是PHP代码

下面是Java代码

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("file/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),
            PICK_IMAGE_REQUEST);
}
我在上传按钮上调用了asynTask

if (v == buttonUpload) {
        // uploadImage();
        new PostDataAsyncTask().execute();
    }
doInBackground中的函数调用是

private void postFile() {
    try {

        // the file to be posted
         String textFile = Environment.getExternalStorageDirectory()
         + "/Woodenstreet Doc.doc";
         Log.v(TAG, "textFile: " + textFile);

        // the URL where the file will be posted
        String postReceiverUrl = "http://10.0.2.2/VolleyUpload/upload.php";
        Log.v(TAG, "postURL: " + postReceiverUrl);

        // new HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        // post header
        HttpPost httpPost = new HttpPost(postReceiverUrl);

        File file = new File(filePath.toString());
        FileBody fileBody = new FileBody(file);

        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("file", fileBody);
        httpPost.setEntity(reqEntity);

        // execute HTTP post request
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {

            String responseStr = EntityUtils.toString(resEntity).trim();
            Log.v(TAG, "Response: " + responseStr);

            // you can add an if statement here and do other actions based
            // on the response
        }

    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
我得到的例外是

java.io.FileNotFoundException: content:/com.topnet999.android.filemanager/storage/0F02-250A/test.doc: open failed: ENOENT (No such file or directory)
emulator-test.doc中有一个文件。 如果代码中有我遗漏的东西,请帮助我。 或者推荐一个将pdf上传到php服务器的教程


提前感谢。

下面的代码没有经过测试(按原样),但通常是如何处理文件上载的-正如您将看到的,其中有一条调试语句。试着发送文件,看看你得到了什么~

   <?php
        /* Basic file upload handler - untested */
        if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES['image'] ) && !empty( $_FILES['image']['tmp_name'] ) ){

            /* Assuming the field being POSTed is called `image`*/
            $name = $_FILES['image']['name'];
            $size = $_FILES['image']['size'];
            $type = $_FILES['image']['type'];
            $tmp  = $_FILES['image']['tmp_name'];


            /* debug:comment out if this looks ok */
            exit( print_r( $_FILES,true ) );

            $result = $status = false;

            $basename=pathinfo( $name, PATHINFO_FILENAME );


            $filepath='http://10.0.2.2/VolleyUpload/'.$basename;

            $result=@move_uploaded_file( $tmp, $filepath );

            if( $result ){
                $sql = "insert into `volleyupload` ( `photo`, `name` ) values ( '$filepath', '$basename' )";
                $status=mysqli_query( $con, $sql );
            }

            echo $result && $status ? 'File uploaded and logged to db' : 'Something not quite right. Uploaded:'.$result.' Logged:'.$status;
        }
    ?>

下面的代码未按原样进行测试,但通常是如何处理文件上载的-正如您将看到的,其中有一条调试语句。试着发送文件,看看你得到了什么~

   <?php
        /* Basic file upload handler - untested */
        if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES['image'] ) && !empty( $_FILES['image']['tmp_name'] ) ){

            /* Assuming the field being POSTed is called `image`*/
            $name = $_FILES['image']['name'];
            $size = $_FILES['image']['size'];
            $type = $_FILES['image']['type'];
            $tmp  = $_FILES['image']['tmp_name'];


            /* debug:comment out if this looks ok */
            exit( print_r( $_FILES,true ) );

            $result = $status = false;

            $basename=pathinfo( $name, PATHINFO_FILENAME );


            $filepath='http://10.0.2.2/VolleyUpload/'.$basename;

            $result=@move_uploaded_file( $tmp, $filepath );

            if( $result ){
                $sql = "insert into `volleyupload` ( `photo`, `name` ) values ( '$filepath', '$basename' )";
                $status=mysqli_query( $con, $sql );
            }

            echo $result && $status ? 'File uploaded and logged to db' : 'Something not quite right. Uploaded:'.$result.' Logged:'.$status;
        }
    ?>

您拥有的是内容提供商路径。不是文件系统路径。
所以你不能使用这个文件。。。上课

改用

  InputStream is = getContentResolver().openInputStream(uri);
其余的php代码没有意义,因为上传时没有base64编码。此外,$path和$actualpath参数未被使用,并且容易混淆。你没有告诉你的脚本应该做什么

您拥有的是内容提供商路径。不是文件系统路径。 所以你不能使用这个文件。。。上课

改用

  InputStream is = getContentResolver().openInputStream(uri);

其余的php代码没有意义,因为上传时没有base64编码。此外,$path和$actualpath参数未被使用,并且容易混淆。您没有告诉脚本应该做什么。

以下是我问题的解决方案:- 下面是php文件-file.php的代码

<?php

// DISPLAY FILE INFORMATION JUST TO CHECK IF FILE OR IMAGE EXIST
echo '<pre>';
print_r($_FILES);
echo '</pre>';

// DISPLAY POST DATA JUST TO CHECK IF THE STRING DATA EXIST
echo '<pre>';
print_r($_POST);
echo '</pre>';

$file_path = "images/";
$file_path = $file_path . basename( $_FILES['file']['name']);

if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) {

    echo "file saved success";


} else{

   echo "failed to save file";
}?>
内部onActivityResult函数

btn_upload.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new PostDataAsyncTask().execute();

        }
    });
这是一项将文件上载到服务器的异步任务

希望这对你有帮助。
很乐意提供帮助,也很乐意编写代码。

以下是我问题的解决方案:- 下面是php文件-file.php的代码

<?php

// DISPLAY FILE INFORMATION JUST TO CHECK IF FILE OR IMAGE EXIST
echo '<pre>';
print_r($_FILES);
echo '</pre>';

// DISPLAY POST DATA JUST TO CHECK IF THE STRING DATA EXIST
echo '<pre>';
print_r($_POST);
echo '</pre>';

$file_path = "images/";
$file_path = $file_path . basename( $_FILES['file']['name']);

if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) {

    echo "file saved success";


} else{

   echo "failed to save file";
}?>
内部onActivityResult函数

btn_upload.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new PostDataAsyncTask().execute();

        }
    });
这是一项将文件上载到服务器的异步任务

希望这对你有帮助。

乐于帮助,乐于编码。

投票反对什么,请解释。你觉得哪一部分不起作用-php还是java?我不知道我犯了什么错误,有一段时间,文件被发送到服务器,但它是空文件。看起来你只是使用
文件内容创建了一个文件
-没有与上传文件相关的常见php,我可以在我的代码中看到我得到了一个在移动设备中放置的文件,现在我必须将此文件发送到php服务器。我做到了,文件是空的。投票支持什么,请解释。你觉得哪个部分不起作用-php还是java?我不知道我在哪里出错,有一段时间,文件被发送到服务器,但它是空文件。看起来你只是使用
文件内容创建了一个文件
-没有与上传文件相关的常见php,我可以在我的代码中看到我得到了一个在移动设备中放置的文件,现在我必须将此文件发送到php服务器。我做到了,文件是空的。第一个是什么,文件1,令牌,?文件1是文件,令牌是StringTokenizer来拆分字符串,第一个是字符串,其中第一个字符串保存在拆分字符串之后,并使用Httpurlconnection,因为httpclient被定价项目不在这里,它只是回答。如果我们使用volley呢?Shanewhat is first,file1,token,?file1是File,token是StringTokenizer来拆分字符串,first是string拆分后保存第一个字符串的字符串,使用Httpurlconnection,因为httpclient是deprivatedproject不在这里,它只是回答。如果我们使用volley呢?谢恩
btn_upload.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new PostDataAsyncTask().execute();

        }
    });