Java 从Android客户端向AppEngine云端点发送图像

Java 从Android客户端向AppEngine云端点发送图像,java,android,google-app-engine,post,google-cloud-endpoints,Java,Android,Google App Engine,Post,Google Cloud Endpoints,我正在开发一个带有AppEngine后端的Android应用程序。我正在用Java创建带有Google云端点的服务器部件。我的问题是无法将位图从客户端发送到服务器 我使用了来自的答案,但即使客户机部分似乎没有任何问题,服务器部分也根本不会接收数据。我还认为这个解决方案可能有点复杂,它可能以另一种更简单的方式工作,但是这是我第一次实现服务器,第一次向它发送图片,所以我接受关于这方面的任何好提示。谢谢 这是我的密码: String boundary = Long.toHexStrin

我正在开发一个带有AppEngine后端的Android应用程序。我正在用Java创建带有Google云端点的服务器部件。我的问题是无法将位图从客户端发送到服务器

我使用了来自的答案,但即使客户机部分似乎没有任何问题,服务器部分也根本不会接收数据。我还认为这个解决方案可能有点复杂,它可能以另一种更简单的方式工作,但是这是我第一次实现服务器,第一次向它发送图片,所以我接受关于这方面的任何好提示。谢谢

这是我的密码:

        String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
        String CRLF = "\r\n"; // Line separator required by multipart/form-data.
        String charset = "UTF-8";

        HttpURLConnection connection = (HttpURLConnection) new URL("https://path_to_my_app/_ah/api/registration/v1/uploadImage").openConnection();
        connection.setDoOutput(true);
        connection.setReadTimeout(60000);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        PrintWriter writer = null;
        try {
            OutputStream output = connection.getOutputStream();
            writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!

            // Send text file.
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + somename + "\"").append(CRLF);
            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
            writer.append(CRLF).flush();
            BufferedReader reader = null;

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
            byteArray = stream.toByteArray();

            try {
                reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(byteArray), charset));
                for (String line; (line = reader.readLine()) != null;) {
                    writer.append(line).append(CRLF);
                }
            } finally {
                if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
            }
            writer.flush();


//            End of multipart/form-data.
        writer.append("--" + boundary + "--").append(CRLF);
    }
    finally
    {
        if (writer != null)
        {
            writer.close();
        }
    }
服务器部分:

@ApiMethod(name = "uploadImage", httpMethod = "POST")
public void uploadImage(HttpServletRequest request, HttpServletResponse response) throws IOException
{
    ServletFileUpload fileUpload = new ServletFileUpload();
    try
    {
        FileItemIterator iterator = fileUpload.getItemIterator(request);

        while(iterator.hasNext()){
            FileItemStream itemStream = iterator.next();

            String fieldName = itemStream.getFieldName();
            log.info("field name:"+fieldName);

            InputStream stream = itemStream.openStream();

            String result = getStringFromInputStream(stream);
            log.info("result: "+result);

            stream.close();
        }
    }
    catch (FileUploadException e)
    {
        e.printStackTrace();
    }
}

我现在没有内容类型。

我做到了

我认为这不是最好的方法,但它很有效,所以我很好,直到我找到更好的解决方案

因此,我将位图图像转换为字符串:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] bitmapByte = outputStream.toByteArray();
String stringEncodedImage = Base64.encodeToString(bitmapByte, Base64.DEFAULT);
然后,我创建一个httpPostRequest,并为其设置一个JsonObject,其中的图像转换为字符串

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://my_app_path/_ah/api/registration/v1/uploadImage");

JSONObject jsonObject = new JSONObject();
jsonObject.put("image",stringEncodedImage);

StringEntity stringEntity = new StringEntity(jsonObject.toString());
httpPost.addHeader("Content-Type", "application/json");
httpPost.setEntity(stringEntity);
HttpResponse response = httpClient.execute(httpPost);
在服务器端,在我的端点中,我执行以下操作:

@ApiMethod(name = "uploadImage", httpMethod = "POST")
public JSONObject uploadImage(JSONObject request) throws IOException
{
    String imageInString = (String) request.get("image");
    Blob blob = new Blob(imageInString.getBytes());
    ....save blob and do whatever you want...
}

反之亦然。我将Blob打包到JsonObject中并发送过来。

将图像转换为base64字符串并存储在服务器上。它会起作用的。看看你举的例子。对于二进制文件的图像,内容类型不能为“text/plain”。那么我应该在那里放置什么?我尝试了多部分/表单数据。我猜问题也可能出在服务器方法参数数据类型上,因此我将其更改为多部分内容类型,并更新了最底部的my question。我只是不知道现在如何在服务器端获取图像。