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

时间:2014-03-15 18:05:57

标签: php android

我正在尝试使用php脚本将Android摄像头捕获的图像上传到服务器。 但是,会发生一个错误。

相关代码是:

// 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)) {
    .....
}
....
?>  

Apache错误日志中的错误是

PHP Notice:  Undefined index: uploadedfile

但是,我已经在uploadPhoto函数中做了声明。

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

我不知道。有任何关于此错误的建议吗? 我还在php脚本中输出$ _FILES和$ _POST。这两个变量都是空的。在Apache错误日志中,也存在错误信息。在Apache访问日志中,服务器响应代码为200.我真的不知道发生了什么以及如何解决它。

0 个答案:

没有答案