【问题标题】:Define a filebody when provided with bytestream of a file's content提供文件内容的字节流时定义文件体
【发布时间】:2012-01-28 13:46:50
【问题描述】:

我正在尝试使用 apache http 客户端 v4.x。

对于特定情况,我必须提出多部分表单请求...

使用以下代码在 java 中发出多部分表单请求--

HttpPost httppost = new HttpPost("http://localhost:8080" + "/servlets-examples/servlet/RequestInfoExample");

FileBody bin = new FileBody(new File(args[0]));
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);

在我的例子中,我有一个文件体的字节流,

byte[] bytes = value.toBinary(charset);

现在我想将此文件(字节流)添加到多部分实体中,以便我可以发出多部分发布请求...我该怎么做?

【问题讨论】:

    标签: java httpclient multipartform-data apache-httpcomponents


    【解决方案1】:

    Using ByteArrayBody?

    byte[] bytes = value.toBinary(charset);
    ContentBody bin = new ByteArrayBody(bytes, "myfile.dat");
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("bin", bin);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 2018-12-09
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多