将jpg图像发送到服务器(json)

时间:2022-01-16 16:51:46

I am trying to write code to send jpg images to a server that can receive JSON requests. This images are stored on the device. I am basically porting a Python code that uses Requests: HTTP for Humans to communicate with a server. So far I managed to establish the communication with the code I pasted below. So I can see some hope.

我正在尝试编写代码以将jpg图像发送到可以接收JSON请求的服务器。该图像存储在设备上。我基本上移植了一个使用Requests的Python代码:人类的HTTP与服务器进行通信。到目前为止,我设法与下面粘贴的代码建立通信。所以我可以看到一些希望。

     // JAVA code on Android
     String URL = BASE_URL + "/relative_url";

     JsonObjectRequest postRequest = new JsonObjectRequest( Request.Method.POST, URL, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i(TAG, "Successful POST request");
                    try {
                        sceneid = response.getInt("id");
                        Log.i(TAG, "Scene id: " + sceneid);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i(TAG, "Bad POST request");
                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<>();
                    String credentials = "myuser:mypass";
                    String auth = "Basic " +  Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                    headers.put("Authorization", auth);
                    return headers;
                }
    };
    queue.add(postRequest);

However, I am not able to replicate the Python code below. I already checked thoroughly for a solution online, but I did not manage to make it work. The solutions I found either use deprecated libraries or the code does not work. The command in python I am trying to replicate is:

但是,我无法复制下面的Python代码。我已经在网上彻底检查了解决方案,但我没有设法让它工作。我发现的解决方案要么使用已弃用的库,要么代码不起作用。我试图复制python中的命令是:

# Python code to replicate in JAVA
auth = {'myuser', 'mypass'}
file = {'jpgdata': open('myimg.jpg','rb')}
req = requests.post(BASE_URL + '/relative_url', params={'param', param}, files=file, auth=auth)

Some help will be really appreciated.

一些帮助将非常感激。

Thank you in advance.

先谢谢你。

2 个解决方案

#1


0  

You will need to read the bytes from that File into a byte[] and put that object into your JSONObject.

您需要将该文件中的字节读入byte []并将该对象放入JSONObject中。

You should also have a look at the following posts :

您还应该看看以下帖子:

ByteArray in JSON

•JSON中的ByteArray

Binary Data in JSON String. Something better than Base64

•JSON字符串中的二进制数据。比Base64更好的东西

BSON library for java

•适用于java的BSON库

Hope this helps.

希望这可以帮助。

#2


0  

 public static String BitMapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = null;
        try {
            System.gc();
            temp = Base64.encodeToString(b, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
            b = baos.toByteArray();
            temp = Base64.encodeToString(b, Base64.DEFAULT);
            Log.e("EWN", "Out of memory error catched");
        }
        return temp;
    }

Use this code and send string of image to server. Hope help you.

使用此代码并将图像字符串发送到服务器。希望能帮助你。

#1


0  

You will need to read the bytes from that File into a byte[] and put that object into your JSONObject.

您需要将该文件中的字节读入byte []并将该对象放入JSONObject中。

You should also have a look at the following posts :

您还应该看看以下帖子:

ByteArray in JSON

•JSON中的ByteArray

Binary Data in JSON String. Something better than Base64

•JSON字符串中的二进制数据。比Base64更好的东西

BSON library for java

•适用于java的BSON库

Hope this helps.

希望这可以帮助。

#2


0  

 public static String BitMapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = null;
        try {
            System.gc();
            temp = Base64.encodeToString(b, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
            b = baos.toByteArray();
            temp = Base64.encodeToString(b, Base64.DEFAULT);
            Log.e("EWN", "Out of memory error catched");
        }
        return temp;
    }

Use this code and send string of image to server. Hope help you.

使用此代码并将图像字符串发送到服务器。希望能帮助你。