简体   繁体   English

Multipart,设置一部分的 Content-Type

[英]Multipart, set Content-Type of one part

I have this code to post data to my server:我有这个代码将数据发布到我的服务器:

// HTTP Settings
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(
                    "http://myserver.com/Login");
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            // Http Headers
            postRequest.addHeader("Accept", "application/xml");
            postRequest.addHeader("Connection", "keep-alive");

            // Credentials
            reqEntity.addPart("username", new StringBody(ServerData.username));
            reqEntity.addPart("password", new StringBody(ServerData.password));

            if (m_sigFile.exists()) {
                Bitmap m_sig = BitmapFactory.decodeFile(sigFilePath
                        + "m_sig.jpg");
                ByteArrayOutputStream m_bao = new ByteArrayOutputStream();
                m_sig.compress(Bitmap.CompressFormat.JPEG, 90, m_bao);

                byte[] m_ba = m_bao.toByteArray();
                String m_ba1 = Base64.encodeToString(m_ba, 0);
                reqEntity.addPart("m_sig.jpg", new StringBody(m_ba1));
            }

            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }

The code works perfectly, all data is send to the server except for the jpeg file.代码完美运行,所有数据都发送到服务器,除了 jpeg 文件。 The server only accepts the file if I set the content type to 'image/jpeg', but only for the image.如果我将内容类型设置为“图像/jpeg”,服务器仅接受该文件,但仅接受图像。 The username and password has to be in plain text.用户名和密码必须是纯文本格式。 Is this possible?这可能吗?

This will work:这将起作用:

            ContentBody cbFile = new FileBody(new File(myPath
                    + "image_1.jpg"),
                    "image/jpeg");
            reqEntity.addPart("photo1"), cbFile);

Don't forget to check if you file exists!不要忘记检查您的文件是否存在!

StringBody有一个接受内容类型的构造函数:

new StringBody(titleString, "application/atom+xml", Charset.forName("UTF-8"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析“多部分/替代”内容类型 - Parsing 'multipart/alternative' content-type 如何在使用RestTemplate(来自其他客户端)时为分段上传中的文件设置内容类型 - How to set content-type for the file in multipart upload when using RestTemplate (from a rest client) "Quarkus-resteasy-multipart 找不到内容类型多部分\/表单数据类型的编写器" - Quarkus-resteasy-multipart could not find writer for content-type multipart/form-data type 设置 Content-Type Azure Blob 存储 - Set Content-Type Azure Blob Storage 无法在SOAP请求上设置Content-Type - Unable to set Content-Type on SOAP Request 如何正确设置Content-Type? - How to set Content-Type correctly? 将表单数据部分中的 Content-Type 指定为 application/json - Specify Content-Type in form-data part as application/json 无法在Java Servlet中设置Content-Type / MIME-type - Cannot set Content-Type / MIME-type in java servlet 如何知道零件请求中零件的实际内容类型是什么 - How to know what is the real content type of the part in multipart request spring rest webservices使用application / json content-type返回multipart / form-data - spring rest webservices return multipart/form-data with application/json content-type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM