HTTPurlconnection多部分上传变量不上传

时间:2015-05-05 19:53:05

标签: php android http

我正在努力使它能够将变量传递给服务器上的php,这将允许我从名为site id的变量创建一个目录。 php在浏览器中使用时有效,但它不能与android程序一起使用。这让我相信变量没有被传递给php。请帮忙

String upLoadServerUri1 = "http://www.fsoportal.com/media/UploadHttp.php";
class TransferBeforePicture extends AsyncTask<String, String, String> {
        protected void onPreExecute() {
            super.onPreExecute();

        }

        protected String doInBackground(String... f_url) {
            SQLiteDatabase myDb;
            myDb = openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
            String selectQuery = "SELECT * FROM " + TABLE_RESULTS + " WHERE SiteID='"+siteID+"' AND QuestionID='"+ SECA + "'";
            Cursor cursor = myDb.rawQuery(selectQuery, null);
            if (cursor.moveToFirst()) {
                do {
                    try {
                        String param = "WORK DAMN YOU";
                        String charset = "UTF-8";
                        HttpURLConnection connection = null;
                        int bytesRead, bytesAvailable, bufferSize;
                        byte[] buffer;
                        int maxBufferSize = 1 * 1024 * 1024;
                        String siteUrl = "http://www.fsoportal.com/media/UploadHttp.php";
                        File sourceFile = new File(cursor.getString(5));
                        String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
                        String CRLF = "\r\n"; // Line separator required by multipart/form-data.
                        URL url = new URL(siteUrl);
                        connection = (HttpURLConnection) url.openConnection();
                        connection.setDoOutput(true);
                        connection.setRequestMethod("POST");
                        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
                        FileInputStream fileInputStream = new FileInputStream(sourceFile);
                        try (
                                OutputStream output = connection.getOutputStream();
                                PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
                        ) {
                            // Send normal param.
                            writer.append("--" + boundary).append(CRLF);
                            writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
                            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
                            writer.append(CRLF).append(param).append(CRLF).flush();

                            // Send text file.
                            writer.append("--" + boundary).append(CRLF);
                            writer.append("Content-Disposition: form-data;  name=\"uploaded_file\"; filename=\"" + cursor.getString(1) + "_" + cursor.getString(2) + "_" + PhotoName + "\"").append(CRLF);
                            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
                            writer.append(CRLF).flush();
                            output.flush();
                            writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
                            bytesAvailable = fileInputStream.available();

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

                            // read file and write it into form...
                            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                            while (bytesRead > 0) {

                                output.write(buffer, 0, bufferSize);
                                bytesAvailable = fileInputStream.available();
                                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                            }
                            // End of multipart/form-data.
                            writer.append("--" + boundary + "--").append(CRLF).flush();
                        }

                        Log.i("uploadFile", "HTTP Response is :  : " + serverResponseCode);

                        if (serverResponseCode == 200) {

                            runOnUiThread(new Runnable() {
                                public void run() {
                                    String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                            + "http://www.fsoportal.com/media/UploadHttp.php";

                                    Toast.makeText(Questionnaire.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                                    finish();

                                }
                            });
                        }

                        //close the streams //
                        fileInputStream.close();

                    } catch (Exception e) {
                        Log.e("Error: ", e.getMessage());
                    }
                } while (cursor.moveToNext());
            }
            return null;

        }

    }

php看起来像这样

    <?php 
$dirname = $_GET['site_id'];
if(!$dirname){
echo "siteid is blank";
exit;
}
$filename = "/home2/ericfso/public_html/media/" . $dirname . "/";

if (!file_exists($filename)) {
    mkdir("./" . $dirname, 0777);
    //echo "The directory $dirname was successfully created.";
    exit;
} else {
    //echo "The directory $dirname exists.";


         $file_path = $folderName."/"; 
     $file_path = $filename . basename( $_FILES['uploaded_file']['name']. '_' . uniqid() . '.jpg'); 
     if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) { 
     echo "success";  
     } else{     
     echo "fail";  
     } 
}
?>

0 个答案:

没有答案