使用Post和httpUrlConnection发送url参数

时间:2013-06-03 10:25:28

标签: php android http

嗨我有一个带有HttpUrlConnection的方法,我正在尝试添加像$_REQUEST['USER'];这样的名称值对

这是我的Android方面

public void sendToCloud(String path) {


    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;

    String pathToOurFile = Environment.getExternalStorageDirectory() + "/PocketCloud/"+path;
    String urlServer = "http://"+ip+"/androidServer/handle_upload.php";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

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

    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

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


    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("USER",this.username.trim()));

    // 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);
    String urlParameters =
            "USER=" + URLEncoder.encode(username, "UTF-8");

    outputStream.writeBytes(urlParameters);


    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("SENDTOCLOUD",serverResponseMessage);
    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    //Exception handling
    }
}

这是我的php方面

<?php

 $user = $_POST['USER'];
 $target_path  = './'.$user.'/';

 $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!";
}
?>

请求无法识别$_POST['USER'] 有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

我通过创建MultipartEntity来管理解决问题,并将文件添加为实体的一部分,将nameValue添加为另一部分。

这篇文章有一个完整的解释:

Sending images using Http Post