多端口和表单数据究竟是什么?他们是如何在android中上传图像的?

多端口和表单数据究竟是什么?他们是如何在android中上传图像的?,android,multipartform-data,Android,Multipartform Data,我搜索了web,但我只能找到与多部分表单数据相关的代码,而没有解释它们是什么以及如何使用它们?通常我们只发送数据的字符串部分,而在多部分文件中,部分添加了字符串,因此称为多部分。例如,我们可以使用截击发送多部分数据 public class MultipartReq extends JsonObjectRequest { private static final String FILE_PART_NAME = "file"; private static fi

我搜索了web,但我只能找到与多部分表单数据相关的代码,而没有解释它们是什么以及如何使用它们?

通常我们只发送数据的字符串部分,而在多部分文件中,部分添加了字符串,因此称为多部分。例如,我们可以使用截击发送多部分数据

 public class MultipartReq extends JsonObjectRequest {

        private static final String FILE_PART_NAME = "file";
        private static final String STRING_PART_NAME = "text";

        private final File mFilePart;
        //private final String mStringPart;


        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        HttpEntity httpEntity;
        Context context;

        private Map<String, String> params;
        public MultipartReq(Context context, int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, File file, Map<String, String> params) {
            super(method, url, jsonRequest, listener, errorListener);

            this.context = context;
            mFilePart = file;
            entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);    
            this.params = params;
            buildMultipartEntity();
            httpEntity = entityBuilder.build();

        }


        private void buildMultipartEntity() {

            try {
                if (mFilePart.exists()) { entityBuilder.addBinaryBody(FILE_PART_NAME, mFilePart, ContentType.create(mimeType), mFilePart.getName());

                    }
                    try {
                        if(!params.isEmpty()){
                            for (String key: params.keySet()){
                                 entityBuilder.addPart(key, new StringBody(params.get(key),ContentType.TEXT_PLAIN));

                            }
                        }
                    } catch (Exception e) {
                        VolleyLog.e("UnsupportedEncodingException");
                    }


                } else {
                    ShowLog.e("no such file");
                }
            } catch (Exception e) {
                ShowLog.e("UnsupportedEncodingException");
            }
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();

            return params;
        }


        @Override
        public String getBodyContentType() {
            return httpEntity.getContentType().getValue();
        }

        @Override
        public byte[] getBody() {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                httpEntity.writeTo(bos);
            } catch (IOException e) {
                VolleyLog.e("IOException writing to ByteArrayOutputStream");
            }
            return bos.toByteArray();
        }


        @Override
        protected void deliverResponse(JSONObject response) {
            super.deliverResponse(response);
        }
    }
public类MultipartReq扩展了JsonObjectRequest{
私有静态最终字符串文件\u PART\u NAME=“FILE”;
私有静态最终字符串字符串\u PART\u NAME=“text”;
私有最终文件mFilePart;
//私有最终字符串mStringPart;
MultipartEntityBuilder entityBuilder=MultipartEntityBuilder.create();
HttpEntity HttpEntity;
语境;
私有映射参数;
公共MultipartReq(上下文上下文、int方法、字符串url、JSONObject jsonRequest、Response.Listener、Response.ErrorListener ErrorListener、文件文件、映射参数){
super(方法、url、jsonRequest、侦听器、errorListener);
this.context=上下文;
mFilePart=文件;
entityBuilder.setMode(与HttpMultipartMode.BROWSER_兼容);
this.params=params;
buildMultipartEntity();
httpEntity=entityBuilder.build();
}
私有void buildMultipartEntity(){
试一试{
if(mFilePart.exists()){entityBuilder.addBinaryBody(文件名、mFilePart、ContentType.create(mimeType)、mFilePart.getName());
}
试一试{
如果(!params.isEmpty()){
for(字符串键:params.keySet()){
addPart(键,新的StringBody(params.get(键),ContentType.TEXT_-PLAIN));
}
}
}捕获(例外e){
截击日志(UnsupportedEncodingException);
}
}否则{
ShowLog.e(“无此类文件”);
}
}捕获(例外e){
ShowLog.e(“不支持的编码例外”);
}
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap params=新的HashMap();
返回参数;
}
@凌驾
公共字符串getBodyContentType(){
返回httpEntity.getContentType().getValue();
}
@凌驾
公共字节[]getBody(){
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
试一试{
httpEntity.writeTo(bos);
}捕获(IOE异常){
e(“向ByteArrayOutputStream写入IOException”);
}
返回bos.toByteArray();
}
@凌驾
受保护的void deliverResponse(JSONObject响应){
super.deliverResponse(响应);
}
}

使用将位图转换为字节[]-

public static byte[] bitmapToBlob(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }

    Log.w("bitmapToBlob", "bitmap getHeight = " + bitmap.getHeight());
    Log.w("bitmapToBlob", "bitmapgetWidth = " + bitmap.getWidth());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.JPEG,
            Some_value_0_to_100, baos);

    return baos.toByteArray();
}
使用此函数以字节[]的形式上载图像

private static void postToUrl3(String url_to_upload_on,
    String file_name_with_ext, byte[] byteArray) {

CloseableHttpClient httpClient = null;

try { 

    httpClient = HttpClientBuilder.create().build();

    HttpPost postRequest = new HttpPost(url_to_upload_on);


    MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
    reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);            
    ByteArrayBody bab = new ByteArrayBody(byteArray, file_name_with_ext);           
    reqEntity.addPart("file", bab);         
    postRequest.setEntity(reqEntity.build());


    httpClient.execute(postRequest);// takes time

} catch (Exception e) {
    Log.w("uploadToBlobStore", "postToUrl Exception e = " + e);
    e.printStackTrace();
} finally { 
    if (httpClient != null) {
        Log.w("uploadToBlobStore", "connection.closing ");
        try { 
            httpClient.close();
        } catch (IOException e) {
            Log.w("uploadToBlobStore", "connection.closing errot e = "
                    + e);
            e.printStackTrace();
        } 
    } 
} 
} 
编辑1-

多部分/表单数据在html中用于发送包含多个部分的数据(顾名思义)。可以向其中添加任何类型的数据,如字符串、文件等。多部分是在java上使用相同内容的CALS。您可以向该实体添加更多数据部分,例如
reqEntity.addPart(“Id”,new StringBody(“Id”)或任何其他类型的实体

但请注意,无论您将此消息发送到哪个服务器,该服务器都应该相应地工作服务器应查找名为“Id”的字段并对其进行处理。如果配置为这样做,服务器也可以进行响应。您可以使用
HttpResponse response=httpClient.execute(postRequest)获得响应


PS各位开发人员请编辑并改进我的答案,以便我也能了解更多信息,或者如果我在
多部分表单中不正确

数据,您也可以使用
字符串
文件
字节
发送数据。它有自己不同类型的主体部分,包括
StringBodyPart
FileBodyPart
ByteArrayBodyPart()
等。它也有一个有限的边界来为您的数据设置。这是你的基本理解!抱歉…回复太晚了…你能解释一下multiPartEntity与multiPart Form dataMultiPart entity的区别吗multiPart entity意味着你可以用不同的主体参数(如字符串、文件和字节等)发送数据。同样,当你看到任何演示代码时,你可以了解更多!你可以随时在我的房间里提问,也可以给我发邮件。我的邮件id在我的堆栈溢出配置文件中也可用