android使用php将图像上传到服务器

android使用php将图像上传到服务器,php,android,Php,Android,我正试图上传图像,这是由安卓摄像头捕捉到的服务器使用php脚本。 但是,出现了一个错误 有关守则如下: // upload photo to server boolean uploadPhoto(FileInputStream fileInputStream) { Log.i(TAG,"step into uploadPhoto"); final String serverFileName = (int) Math .round(Math.random() *

我正试图上传图像,这是由安卓摄像头捕捉到的服务器使用php脚本。 但是,出现了一个错误

有关守则如下:

// upload photo to server
boolean uploadPhoto(FileInputStream fileInputStream) {
    Log.i(TAG,"step into uploadPhoto");

    final String serverFileName = (int) Math
        .round(Math.random() * 1000) + ".jpg";
    Log.i(TAG, "the name of uploaded image is " + serverFileName);
    final String lineEnd = "\r\n";
    final String twoHyphens = "--";
    final String boundary = "*****";

    try {
    URL url = new URL(SERVERURL);
    // Open a HTTP connection to the URL
    final HttpURLConnection conn = (HttpURLConnection) url
        .openConnection();
    // Allow Inputs
    conn.setDoInput(true);
    // Allow Outputs
    conn.setDoOutput(true);
    // Don't use a cached copy.
    conn.setUseCaches(false);

    // Use a post method.
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setRequestProperty("Content-Type",
        "multipart/form-data;boundary=" + boundary);

    DataOutputStream dos = new DataOutputStream(
        conn.getOutputStream());

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
        + serverFileName + "\"" + lineEnd);
    dos.writeBytes(lineEnd);

    // create a buffer of maximum size
    int bytesAvailable = fileInputStream.available();
    int maxBufferSize = 1024;
    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte[] buffer = new byte[bufferSize];

    // read file and write it into form...
    int 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 after file data...
    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    publishProgress(SERVER_PROC_STATE);
    // close streams
    fileInputStream.close();
    dos.flush();
    Log.i(TAG, "upload succeddfully");
    conn.disconnect();
    return true;
    } catch (MalformedURLException ex) {
    Log.e(TAG, "error: " + ex.getMessage(), ex);        
    return false;
    } catch (IOException ioe) {
    Log.e(TAG, "error: " + ioe.getMessage(), ioe);      
    return false;
    }
}
php脚本是:

<?php
....
$photo_upload_path = "./upload/";
$photo_upload_path = $photo_upload_path. basename( $_FILES['uploadedfile']['name']);
if(copy($_FILES['uploadedfile']['tmp_name'], $photo_upload_path)) {
    .....
}
....
?>  
但是,我在uploadPhoto函数中做了声明

dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
        + serverFileName + "\"" + lineEnd);
我不知道。对这个错误有什么建议吗
我还在php脚本中输出了$_文件和$_POST。这两个变量都是空的。在Apache错误日志中,错误信息也存在。在Apache访问日志中,服务器响应代码是200。我真的不知道发生了什么以及如何修复它。

如果你在PHP脚本中输入一个print\u r$\u文件,可能还有一个print\u r$\u帖子,并在android代码中记录响应,你可以检查流量。在这些情况下,仅从代码中很难找到错误。感谢您的回复。我已经测试了一些方法。但是,错误也存在。我还创建了一个test.html,它允许我手动上传图片,效果很好。所以这个bug可能存在于android端。。。我不知道为什么$\u文件['uploadfile']['temp\u name']为空,我还在php脚本中输出$\u文件和$\u POST。这两个变量都为空。在Apache错误日志中,错误信息也存在。在Apache访问日志中,服务器响应代码是200。我真的不知道发生了什么以及如何修复它。
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
        + serverFileName + "\"" + lineEnd);