1

我正在尝试将 img 从 android 手机上传到 php 服务器。

但是当它上传它的数据丢失(文件存在但有0字节)

我不知道问题出在 php 端还是 android 端。

这是我使用上传的代码

PHP:

<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>

爪哇:

private  void httpPostFileUpload(String fpath){
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;

        String pathToOurFile = fpath;
        Log.v("file path",fpath.toString());
        String urlServer = "http://leojg.alwaysdata.net/tests/androidConnect/upload_image.php";
        //String urlServer = "http://65.182.110.63/public/handle_upload.php";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";
        String fname = "";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;

        try
        {
            File f = new File(pathToOurFile);
            fname =  f.getName();

            FileInputStream fileInputStream = new FileInputStream(f);

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

            outputStream = new DataOutputStream( connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            Log.v("ouput stream",outputStream.toString());              

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            Log.d("amanda", "upload async finished, server response : " + serverResponseMessage);

            fileInputStream.close();
            fileInputStream = null;
            outputStream.flush();
            outputStream.close();
            outputStream = null;
            f= null;



            parent.invokeJs("pictureUploadEnded('" + fname + "')");
            //this.delete(fpath);

        }
            catch (Exception ex)
        {
                Log.d("amanda", "exception uploading image : " + ex.getMessage());
        }     
}

在这里你可以看到 img 的存储位置http://leojg.alwaysdata.net/tests/androidConnect/

解决方案:

<?php
$target_path = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
} else{
 echo "There was an error uploading the file, please try again!";
}
?>
4

3 回答 3

1

您无法从$_REQUEST. 试试$_FILEShttp://php.net/manual/en/features.file-upload.php

于 2013-01-22T21:49:22.463 回答
1

你必须使用PHP上传吗?我总觉得 FTP 上传更容易实现(后端可以使用 FileZilla)而且更稳定!

于 2013-01-23T03:08:23.723 回答
0

尝试以下图像加载方法:

http://loopj.com/android-async-http/

让我知道它是否有效。

于 2013-01-22T21:53:59.240 回答