3

我想从 Android 上的相机捕获图像,并将其发送到 Google App Engine,它将图像存储在 blob 商店中。听起来很简单,我可以让 GAE 的多部分 POST 发生,但是存储到 Blob 存储需要 servlet 返回 HTTP 重定向 (302)。因此,我需要一个可以在执行 HTTP POST 后遵循重定向的连接。这是我希望可以工作的代码

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。不过,图像上传得很好。如果有人能告诉我我能做些什么来让它遵循重定向以便我可以阅读响应,我将非常感激。

或者,如果有更好的方法,我很想听听。我知道那里有像 Apache HTTP 客户端这样的库,但它需要如此多的依赖项,这对于一个简单的任务来说似乎过于臃肿。

谢谢

4

2 回答 2

4

几个月前,我试图做同样的事情!

基本上,不要使用HttpURLConnection. 而是在作为 Android SDK 一部分的包中使用HttpClient和。HttpGetorg.apache.http

不幸的是,我没有提供示例的源代码,但希望这会让您朝着正确的方向前进。

于 2010-07-17T08:14:26.647 回答
0

不知道出了什么问题,但您可以自己关注重定向。取出 Location 标头并与之建立新的连接。

于 2010-07-17T06:31:37.640 回答