从Android应用

时间:2015-04-23 16:06:38

标签: php android google-app-engine google-cloud-storage

我正在尝试从我的Android应用上传文件到Google云端存储。我按照说明here

这是我的代码:

public static void uploadFileToServer(String localFilename) {

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(localFilename);

    try {

            // Get the application specific upload URL
            HttpClient httpClient = new DefaultHttpClient();

            HttpGet httpGet = new HttpGet("http://<my-app-id>.appspot.com/createUploadUrl.php");
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity urlEntity = response.getEntity();
            InputStream in = urlEntity.getContent();
            String str = "";
            while (true) {
                int ch = in.read();
                if (ch == -1)
                    break;
                str += (char) ch;
            }

到此为止,一切似乎都没问题。在&#34; str&#34;我得到&#34创建的URL;)CloudStorageTools :: createUploadUrl(&#34 ;,类似于http://.appspot.com/_ah/upload/AMmfu6Yqu4-6KDv4iqomFbHB5rP3SeDG4FYNVvtIX-FfZd0jtNU9PUHCxfzMLU75-81hNYQR_FIOp1xLnIDY-bj1X6r0pcSP0vBGy4-PZhXfSzzSjfsHpEohmVgZm29ULtqXIzcyN1DYxvgR3B8vLRu4Y-BN8Pgd5N16s00zJIVD0bUQXy_bvpA/ ALBNUaYAAAAAVTjyDDQ250PQnVbkaLUZydeL9_gbbWs - /

然后,我尝试将文件上传(发布)到该网址(在允许的10分钟内):

            URL url = new URL(str);

            FileInputStream fileInputStream = new FileInputStream(sourceFile);

            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploaded_file", "whatever.jpg");

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


            //Adding Parameter filename in server
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                            + "Thisfile.jpg" + "\"" + lineEnd);
            dos.writeBytes(lineEnd);

            // create a buffer of  maximum size
            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) {

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

            }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = conn.getResponseCode();

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


    } catch (Exception e) {
        e.printStackTrace();
    }
}

但是文件没有上传。

这是&#34; createUploadUrl.php&#34;中的代码:

<?php
    require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
    use google\appengine\api\cloud_storage\CloudStorageTools;

    $options = [ 'gs_bucket_name' => 'my_bucket' ];  // Here using my bucket name
    $upload_url = CloudStorageTools::createUploadUrl('/UploadToServer.php', $options);

    echo $upload_url;
?>

&#34; UploadToServer.php&#34;:

中的代码
<?php
    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], 'gs://<my_bucket>/new_file.jpg');
?>

有趣的是,此代码适用于任何其他Apache服务器(没有createUploadUrl部分),但不适用于Google云端存储。

我认为&#34; $ _ FILES&#34;可能无法转发到&#34; UploadToServer.php&#34;。

任何提示?

0 个答案:

没有答案