【问题标题】:Java Upload File via HTTP POST requestJava 通过 HTTP POST 请求上传文件
【发布时间】:2016-10-03 15:44:30
【问题描述】:

我正在尝试创建一个 java 应用程序来执行以下在 Windows 上的 CURL 中工作的操作。

curl -x XXX.XXX.XXX.XXX:8080 -X POST --data-binary "@C:\Users\XXXXX\Documents\batch2.txt" http://XXXXX.XXX.XXXXXX.XXX/XX/XXXXXXX/XX/XXXXXXX/XXXXXXX

我已经能够复制获取文件内容的调用,并使用 HttpURLConnection 将其直接发布到 java 中的 URL,但是这是不可行的,我更喜欢使用 file 方法,因为它可能相当大。

我目前拥有的代码是:

import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.mime.HttpMultipartMode;



public class PostFile {
  public static void main(String[] args) throws Exception {

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("http://XXXXX.XXX.XXXXXX.XXX/XX/XXXXXXX/XX/XXXXXXX/XXXXXXX");

    RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
    .setProxy(new HttpHost("XXX.XXX.XXX.XXX", 8080))
    .build();
    httppost.setConfig(requestConfig);

    httppost.addHeader("Content-Type","data/binary");

    File file = new File("C:/Users/XXXXXX/Documents/batch2.txt");

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addBinaryBody("batch", file);

    final HttpEntity entity = builder.build();

    httppost.setEntity(entity);

    System.out.println("executing request " + httppost.getRequestLine() + httppost.getConfig());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    System.out.println(response.getStatusLine());
    if (resEntity != null) {;
      System.out.println(EntityUtils.toString(resEntity));
    }

    httpclient.close();
  }
}

我遇到的问题是它没有提供相同的响应,并且似乎无法 POST 文件。

【问题讨论】:

  • --data-binary 隐含-X POST,因此您不需要提供-X 选项,它还隐含Content-Type: application/x-www-form-urlencoded(请参阅curl manpage),但您的Java 代码正在发送Content-Type: data/binary,这可能是你的问题。至于“没有提供相同的回复”,如果您将这两个回复分享给我们,会更容易为您提供帮助。
  • 成功响应 = Batch-id-XXXXX 错误响应 = 批处理为空
  • 我的意思是,显示完整的响应,包括标题。无论如何,我刚刚注意到您的 Java 代码也以 multi-part 的形式发送文件。您似乎认为curl 命令正在“上传”文件。它不是。它只是将文件作为表单编码的 POST 内容发送到服务器,这意味着文件应该遵循该内容格式。在 Java 中您需要做的就是设置正确的 Content-Type 并直接流式传输文件内容。

标签: java curl http-post


【解决方案1】:

感谢 Andreas 对内容类型的提示,

我已对此进行了更改,并从 MultiPart Entity 更改为 FileEntity,现在可以正常工作了。

import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.mime.HttpMultipartMode;



public class PostFile {
  public static void main(String[] args) throws Exception {

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("http://XXXXX.XXX.XXXXXX.XXX/XX/XXXXXXX/XX/XXXXXXX/XXXXXXX");

    RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
    .setProxy(new HttpHost("XXX.XXX.XXX.XXX", 8080))
    .build();
    httppost.setConfig(requestConfig);

    httppost.addHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");

    File file = new File("batch2.txt");

    FileEntity entity = new FileEntity(file);

    httppost.setEntity(entity);

    System.out.println("executing request " + httppost.getRequestLine() + httppost.getConfig());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    System.out.println(response.getStatusLine());
    if (resEntity != null) {;
      System.out.println(EntityUtils.toString(resEntity));
    }

    httpclient.close();
  }
}

【讨论】:

    猜你喜欢
    • 2018-12-13
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多