发送图像从Android客户端来的AppEngine云端点云端、端来、图像、客户

2023-09-05 10:43:58 作者:远处看不尽的风景

我开发一个Android应用程序了与AppEngine上后端。我创建与谷歌云端点在Java中的服务器部分。我的问题是我无法从客户端发送一个位图到服务器。

I am developing an Android applicaiton with AppEngine backend. I am creating the server part with Google Cloud Endpoints in Java. My problem is that I cannot send a Bitmap from the client to the server.

我使用的答案从这个问题的但即使客户端部分似乎不具有在所有的任何问题,服务器部分不接收的数据在所有。我也觉得这个解决方案可能是一个有点复杂,它可能工作不同的,更简单的方法,但是这是我第一次实现服务器与第一次发送图片到它,所以我接受这个任何好的建议。谢谢!

I used the answer from this question but even if the client part does not seem to have any problems at all, the server part does not receive the data at all. I also think this solution might be a bit complicated and that it might work a different, easier way, however this is my first time implementing a server and first time sending a picture to it so I accept any good tips on this. Thanks!

下面是我的code:

        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();
        }
    }

服务器部分:

The server part:

@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();
    }

}

我收到204目前没有内容类型。

I am getting 204 no Content type now.

推荐答案

我做到了!

我觉得这是不是做的最好的方式,但it's工作,所以我很好,直到我得到一个更好的解决方案。

I think this is not the best way of doing it but it´s working so I am fine until I get a better solution.

所以,我把位图图像并将其转换为字符串:

So I take the Bitmap image and convert it to String:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] bitmapByte = outputStream.toByteArray();
String stringEncodedImage = Base64.encodeToString(bitmapByte, Base64.DEFAULT);

然后,我创建了一个httpPostRequest和设置的JSONObject将其与图像转换为字符串吧。

Then I create a httpPostRequest and set a JsonObject to it with the image converted to String in it.

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);

在服务器端,在我的终点,我这样做:

On the server side, in my Endpoint, I do this:

@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的,它送过来。

The same goes the other way. I pack Blob into JsonObject and send it over.

 
精彩推荐
图片推荐