gpt4 book ai didi

Java通过HTTP POST请求上传文件

转载 作者:行者123 更新时间:2023-12-01 09:25:50 25 4
gpt4 key购买 nike

我正在尝试创建一个 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();
}
}

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

最佳答案

感谢 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();
}
}

关于Java通过HTTP POST请求上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39835467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com