代码之家  ›  专栏  ›  技术社区  ›  Peter Sankauskas

最佳方法:从Android到GAE的httppost(多部分)

  •  3
  • Peter Sankauskas  · 技术社区  · 14 年前

    我想在Android上从相机上捕获一个图像,并将其发送到googleappengine,googleappengine将图像存储在blob商店中。听起来很简单,我可以将多部分POST发送到GAE,但是存储到Blob存储区需要servlet返回HTTP重定向(302)。所以,我需要一个连接,可以遵循重定向后,做了一个HTTP职位。这是我的密码 愿望

    public static String sendPhoto(String myUrl, byte[] imageData) {
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        String pathToOurFile = "/data/file_to_send.jpg";
        String urlServer = myUrl;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        // Please follow redirects
        HttpURLConnection.setFollowRedirects(true);
        BufferedReader rd = null;
        StringBuilder sb = null;
        String line = null;
        try {
            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();
            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            // I really want you to follow redirects
            connection.setInstanceFollowRedirects(true);
            connection.setConnectTimeout(10000); // 10 sec
            connection.setReadTimeout(10000); // 10 sec
            // Enable POST method
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);
            connection.connect();
            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream
                    .writeBytes("Content-Disposition: form-data; name="
                        + "\"file1\";filename=\""
                        + pathToOurFile + "\"" + lineEnd);
            outputStream.writeBytes(lineEnd);
            outputStream.write(imageData);
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                + lineEnd);
            outputStream.flush();
            outputStream.close();
            rd = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            sb = new StringBuilder();
            while ((line = rd.readLine()) != null) {
                sb.append(line + '\n');
            }
            Log.i("Response: ", sb.toString());
            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();
            Log.i("Response: serverResponseCode:",
                String.valueOf(serverResponseCode));
            Log.i("Response: serverResponseMessage:", serverResponseMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    

    尽管我尝试了很多,但这段代码不会遵循重定向。输入蒸汽总是空的,响应总是302。图片上传得很好。如果有人能告诉我,我可以做什么,使它遵循重定向,这样我就可以阅读的答复,我真的很感激。

    谢谢

    2 回复  |  直到 11 年前
        1
  •  4
  •   Dave    14 年前

    几个月前我也在试着做同样的事情!

    HttpURLConnection HttpClient HttpGet org.apache.http

    不幸的是,我没有我的源代码来提供一个例子,但希望这能为您指明正确的方向。

        2
  •  0
  •   irreputable    14 年前

    不知道出了什么问题,但你可以跟着自己。取出位置标头并与之建立新连接。