如何在Java中使用HttpURLConnection发送多部分POST请求? [英] How to send multipart POST request using HttpURLConnection in java?

查看:356
本文介绍了如何在Java中使用HttpURLConnection发送多部分POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次发送多部分请求,在这里进行挖掘之后,我变得更加困惑,因此任何有关正确"方式的帮助都将非常有用.

It's my first time sending multipart requests and after digging here, I got even more confused so any help regarding the "correct" way will be very appriciated.

我有一个函数,该函数应该获取:文件路径和JSON的String表示形式,并使用multipart向服务器发送POST请求.

I have a function, that should get : file path and a String representation of JSON and send POST request to the server using multipart.

我不确定何时使用boundary"multipart/form-data"内容类型,以及addPartaddTextBody之间的区别,以及何时(或为什么)总是写Content-Disposition: form-data; name=\

I'm not sure when to use the boundary and "multipart/form-data" content type, and the difference between addPart and addTextBody, and when (or why) it is always written Content-Disposition: form-data; name=\

public String foo(String filePath, String jsonRep, Proxy proxy)
{
   File f = new File(filePath);
   HttpURLConnection connection;
   connection = (HttpURLConnection) url.openConnection(proxy);
   connection.setRequestProperty("Content-Type", "multipart/form-data"); // How should I generate boundary? Should it be added here? 

    if (myMethod == "POST")
     {
       connection.getOutputStream().write( ? Both the json string and the file bytes?? );
      }


 .... checking there is no error code etc..

 return ReadResponse(connection) // read input stream..

现在我不确定如何继续以及如何写入文件和json字符串 我看到了这段代码:

Now I'm not sure how to continue, and how to write the file and the json string I saw this code:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
builder.addPart("text1", stringBody1);
builder.addPart("text2", stringBody2);

但是我似乎无法理解它如何连接到我的connection.

But I can't seem to understand how it is connected to my connection.

可以帮忙吗?

推荐答案

示例HTML表单:

<form method="post" action="http://127.0.0.1/app" enctype="multipart/form-data">
<input type="text" name="foo" value="bar"><br>
<input type="file" name="bin"><br>
<input type="submit" value="test">
</form>

用于提交多部分表单的Java代码:

Java code for submiting the multipart form:

    MultipartEntityBuilder mb = MultipartEntityBuilder.create();//org.apache.http.entity.mime
    mb.addTextBody("foo", "bar");
    mb.addBinaryBody("bin", new File("testFilePath"));
    org.apache.http.HttpEntity e = mb.build();

    URLConnection conn = new URL("http://127.0.0.1:8080/app").openConnection();
    conn.setDoOutput(true);
    conn.addRequestProperty(e.getContentType().getName(), e.getContentType().getValue());//header "Content-Type"...
    conn.addRequestProperty("Content-Length", String.valueOf(e.getContentLength()));
    OutputStream fout = conn.getOutputStream();
    e.writeTo(fout);//write multi part data...
    fout.close();
    conn.getInputStream().close();//output of remote url

这篇关于如何在Java中使用HttpURLConnection发送多部分POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆