Java上传文件通过HTTP POST请求

时间:2016-10-03 15:44:30

标签: java curl http-post

我正在尝试创建一个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,但这不可行,并且更喜欢使用文件方法,因为它可能相当大。

我目前的代码是:

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文件。

1 个答案:

答案 0 :(得分:1)

感谢Andreas暗示内容类型,

我已将此更改并从MultiPart实体更改为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();
  }
}