首页 文章

Http POST 如何使用 multipart/form-data 发送带有文件的 JSON

提问于
浏览
0

我无法弄清楚如何使用 multipart/form-data 发送带有图像的 JSON 对象。

POST /api/user/update 
{  id: 123,  
   user: { logo: !!here_file!!  }
}

我试图将 base64 字符串放入徽标字段,并且只是传递这个 json 对象,但这种方法不起作用,服务器需要 content-type:multipart/form-data;我无法得到如何做到这一点。我已经查看了很多问题,但没有找到如何使用文件发布 JSON,以及此文件。

2 回答

  • 0

    这是我用来将 POST 发送到后端的通用方法:

    /**
     * putJSONObject
     *
     * @param url
     * @param jsonObject
     * @param timeoutMillis
     * @return
     */
    protected static JSONObject putJSONObject (String url, JSONObject jsonObject, int timeoutMillis) throws IOException, JSONException {
    
        StringBuilder stringBuilder = new StringBuilder ();
    
        HttpURLConnection httpURLConnection;
        DataOutputStream printout;
    
        httpURLConnection = (HttpURLConnection) new URL (url).openConnection ();
        httpURLConnection.setRequestMethod ("POST");
        httpURLConnection.setReadTimeout (timeoutMillis);
        httpURLConnection.setConnectTimeout (timeoutMillis);
        httpURLConnection.setDoInput (true);
        httpURLConnection.setDoOutput (true);
        httpURLConnection.setUseCaches (false);
        httpURLConnection.connect ();
    
        // Send POST output.
        printout = new DataOutputStream (httpURLConnection.getOutputStream ());
        printout.writeBytes ("msg=" + URLEncoder.encode (jsonObject.toString (), "UTF-8"));
        printout.flush ();
        printout.close ();
    
        InputStreamReader inputStreamReader = new InputStreamReader (httpURLConnection.getInputStream ());
    
        int read;
        char[] buff = new char[4096];
        while ((read = inputStreamReader.read (buff)) != -1) {
            stringBuilder.append (buff, 0, read);
        }
        httpURLConnection.disconnect ();
    
        return new JSONObject (stringBuilder.toString ());
    
    }
    

    发送的 JSON 以'msg'的形式发送

    并将图片编码为字符串这是我的代码:

    /**
     * toBase64
     *
     * @param bitmap
     * @return String
     */
    public static String toBase64 (Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage;
    }
    
    /**
     * fromBase64
     *
     * @param encodedImage
     * @return Bitmap
     */
    public static Bitmap fromBase64 (String encodedImage) {
        byte[] decodedByte = Base64.decode(encodedImage, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    }
    

    希望它能帮到你。

  • 0

    首先你应该改变方法。在您向任何地方发送 JSON 对象之前,您必须将图像(文件)加载到上载服务器。上传服务器它是一个服务器,您可以在其中存储您的图像,并可以通过引用访问它。它看起来像这样:使用 multipart/form-data 将图像上传到服务器并获取图像链接。然后将此链接放入您的 JSON 对象中

    {  
       id: 123,  
       user: { logo: https://myuploadserver.com/img123.jpg }
    }
    

    然后你可以使用你想要的 JSON 对象

    一些指向 stackoverflow 的链接,其中包含如何使用 multipart/form-data 将数据上传到上传服务器的说明:

    1.简单的 HttpURLConnection POST 文件 multipart/form-data
    2.将多部分表单数据上载到服务器

相关问题