簡體   English   中英

從Android對Google App Engine Java Blobstore上傳無響應

[英]No Response on Google App Engine Java Blobstore Upload from Android

我正在嘗試將圖像從Android上傳到blobstore,但是當服務器servlet完成調用時,Blobstore無法傳遞任何BlobKey。 當我檢查實際的Blobstore時,沒有圖像上傳。 沒有錯誤代碼,沒有警告,沒有錯誤,什么都沒有。 現在我知道服務器上的東西可以工作了,因為上傳可以在iOS中工作。 而且,Android代碼在本地開發服務器上也可以正常工作,但無法在生產環境中使用。 我希望有人可以在我的代碼中發現錯誤。

提前致謝。

HttpURLConnection connection = null;

    try {

        Log.d(LOG_TAG, "Starting multipart POST request to: " +fullUrlValue);

        URL fullUrl = new URL(fullUrlValue);

        //make server call
        connection = getClient().getConnection(fullUrl, RequestMethod.POST);
        connection.setConnectTimeout(60000); //60 seconds to complete an upload 
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //see if we are streaming the upload
        if(requestOptions == null || requestOptions.isChunckedStreamingMode())
        {
            connection.setChunkedStreamingMode(2048);
        }

        String boundry = "z6fQbdm2TTgLwPQj9u1HjAM25z9AJuGSx7WG9dnD";

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundry);

        //attach post objects
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());

        //add form fields
        if(additionalParameters != null && additionalParameters.keySet().size() > 0)
        {
            Iterator<String> it = additionalParameters.keySet().iterator();
            while(it.hasNext())
            {
                String fieldName = it.next();
                if(fieldName != null)
                {
                    String value = additionalParameters.get(fieldName);
                    if(value != null)
                    {
                        //add form field to upload form
                        outputStream.writeBytes("--" + boundry + "\r\n" + "Content-Disposition: form-data; name=\"" + fieldName + "\"\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\n"+value+"\r\n");
                    }
                }
            }
        }

        //attach file
        if(postObjectBytes != null)
        {   
            //build the request body
            outputStream.writeBytes("--" + boundry + "\r\n" + "Content-Disposition: form-data; name=\"" + formElementName + "\";filename=\"" + fileName + "\"\r\n\r\n");


            //object upload content size
            long totalUploadSize = postObjectBytes.length;

            //we have to manually process the stream in order to correctly update the progress listener
            byte[] buffer = new byte[2048];
            long totalBytes = 0;
            int bytesRead = 0;

            InputStream inputStream = new ByteArrayInputStream(postObjectBytes);

            while ((bytesRead = inputStream.read(buffer)) > -1) {
                totalBytes += bytesRead;
                outputStream.write(buffer, 0, bytesRead);

                if(progressListener != null){
                    progressListener.transferred(totalBytes, totalUploadSize);
                }
            }

            outputStream.writeBytes("\r\n--" + boundry + "--\r\n");

        }

        outputStream.flush();
        outputStream.close();

好了,由於AppEngine BlobStore上載失敗並在開發環境中起作用的請求 ,我找到了答案。

顯然我在分號和“文件名”開頭之間缺少空格。 那太瘋狂了。

formElementName + "\";filename

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM