Java Spring RestTemplate:禁止获取403多部分/表单数据,但使用curl
我正在使用spring rest模板发布一个文件和一个字符串参数的请求Java Spring RestTemplate:禁止获取403多部分/表单数据,但使用curl,java,spring,multipartform-data,resttemplate,http-status-code-403,Java,Spring,Multipartform Data,Resttemplate,Http Status Code 403,我正在使用spring rest模板发布一个文件和一个字符串参数的请求表单数据,下面是代码: HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Bearer ..."); httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); File file=new File("src\main\resources\test
表单数据
,下面是代码:
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer ...");
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
File file=new File("src\main\resources\test.json");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
byte[] fileBytes = toByteArray(in);
MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
ContentDisposition contentDisposition = ContentDisposition
.builder("form-data")
.name("file")
.filename("test.json")
.build();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
fileMap.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
org.springframework.http.HttpEntity<byte[]> fileEntity =
new org.springframework.http.HttpEntity<>(fileBytes, fileMap);
MultiValueMap< String, Object > body = new LinkedMultiValueMap<>();
body.add("stringFormInput", "xyz");
body.add("file", fileEntity);
org.springframework.http.HttpEntity< MultiValueMap< String, Object > > entity =
new org.springframework.http.HttpEntity<>(body, httpHeaders);
ResponseEntity< JsonNode > responseEntity =
restTemplate.postForEntity("https://uploadFile", entity, JsonNode.class);
我也试过使用httpClient
,但仍然是403,没有任何原因消息
HttpPost post = new HttpPost("https://uploadFile");
post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE);
post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer ...");
File file=new File("src\main\resources\test.json");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, "test.json");
builder.addTextBody("stringFormInput", "xyz", ContentType.DEFAULT_BINARY);
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(post);
编辑
现在,我通过从JAVA执行curl来解决问题
File file=new File("src\main\resources\test.json");
String[] command = {"curl", "--location",
"--request", "POST", "https://uploadFile",
"--header", "Content-Type: multipart/form-data",
"--header", "Authorization: Bearer ...",
"-F", "file=" + "@"+file.getPath(),
"--form", "stringFormInput=xyz"};
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
String curlResult = "";
String line = "";
try {
Process process = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (true) {
line = r.readLine();
if (line == null) {
break;
}
curlResult = curlResult + line;
}
} catch (Exception e) {
e.printStackTrace();
}
所以我遇到了一个类似的问题,cURL执行正常,但是Spring代码给出了403。 问题在于接收此请求的目标。像Nginx这样的web服务器很少需要默认情况下可以禁用的“用户代理”这样的强制头 默认情况下,Spring不会传递任何额外的头。如果目标正在使用Ngnix或查找他们正在使用的web服务器,请尝试添加“用户代理” 分享我的代码以供参考
您需要为httpclient请求添加标题。添加了
内容类型
和授权
标题,仍然是相同的问题此问题可能会有帮助是的,我也尝试了。我尝试了标题(“用户代理”,“PostmanRuntime/7.26.3”)
仍然给出403,是的,服务器是ngnix。
File file=new File("src\main\resources\test.json");
String[] command = {"curl", "--location",
"--request", "POST", "https://uploadFile",
"--header", "Content-Type: multipart/form-data",
"--header", "Authorization: Bearer ...",
"-F", "file=" + "@"+file.getPath(),
"--form", "stringFormInput=xyz"};
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
String curlResult = "";
String line = "";
try {
Process process = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (true) {
line = r.readLine();
if (line == null) {
break;
}
curlResult = curlResult + line;
}
} catch (Exception e) {
e.printStackTrace();
}