作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!