clamav-rest Spring 3.2 RestTemplate 导致 400 Bad Request
clamav-rest Spring 3.2 RestTemplate results in 400 Bad Request
我正在尝试将文件发送到 Spring 启动应用程序 clamav-rest。 curl 命令工作正常。这是输出,
curl -k -F name="test file" -F file=@"myfile.pdf" https://clamav.my.org/scan
POST /scan HTTP/1.1
Host: clamav.my.org
User-Agent: curl/7.56.1
Accept: */*
Content-Length: 22834
Content-Type: multipart/form-data; boundary=------------------------5814453ea3a5f3e0
Expect: 100-continue
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Thu, 18 Jan 2018 23:19:10 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 21
Everything ok : true
我正在使用 Spring 3.2 进行 REST 调用,这会导致 400 Bad Request 错误。
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("name", "fileUpload.obj");
parts.add("file", in);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity =
new HttpEntity<MultiValueMap<String, Object>>(parts, headers);
ResponseEntity<String> responseEntity =
restTemplate.exchange("https://clamav.my.org/scan", HttpMethod.POST, requestEntity, String.class);
String body = (String) responseEntity.getBody();
这是日志输出
Created POST request for "https://clamav.my.org/scan"
Setting request Accept header to [text/plain, application/json, application/*+json, */*]
Writing [{name=[fileUpload.obj], file=[[B@fe97ef91]}] as "multipart/form-data" using [org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@4885d075]
POST request for "https://clamav.my.org/scan" resulted in 400 (Bad Request); invoking error handler
Created POST request for "https://clamav.my.org/scan"
Setting request Accept header to [text/plain, application/json, application/*+json, */*]
Writing [{file=[[B@fe97ef91]}] using [org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@e8e8b3fa]
POST request for "https://clamav.my.org/scan" resulted in 400 (Bad Request); invoking error handler
我已经尝试了几次迭代并搜索了示例代码。关于如何成功请求的任何想法? TIA
在相关的 post、sending-file-over-spring-rest-service-via-resttemplate
中找到了答案
现在调用 RestTemplate,
RestTemplate restTemplate = new RestTemplate();
FormHttpMessageConverter converter = new FormHttpMessageConverter();
restTemplate.getMessageConverters().add(converter);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultipartByteArrayResource resource = new MultipartByteArrayResource(in);
resource.setFilename("fileUpload.obj");
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("name", "fileUpload.obj");
parts.add("file", resource);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(parts,
headers);
ResponseEntity<String> responseEntity = restTemplate.exchange("https://clamav.my.org/scan",
HttpMethod.POST, requestEntity, String.class);
String body = (String) responseEntity.getBody();
我相信这可以改进,但它目前正在工作。
我正在尝试将文件发送到 Spring 启动应用程序 clamav-rest。 curl 命令工作正常。这是输出,
curl -k -F name="test file" -F file=@"myfile.pdf" https://clamav.my.org/scan
POST /scan HTTP/1.1 Host: clamav.my.org User-Agent: curl/7.56.1 Accept: */* Content-Length: 22834 Content-Type: multipart/form-data; boundary=------------------------5814453ea3a5f3e0 Expect: 100-continue HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Thu, 18 Jan 2018 23:19:10 GMT Content-Type: text/plain;charset=utf-8 Content-Length: 21 Everything ok : true
我正在使用 Spring 3.2 进行 REST 调用,这会导致 400 Bad Request 错误。
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("name", "fileUpload.obj");
parts.add("file", in);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity =
new HttpEntity<MultiValueMap<String, Object>>(parts, headers);
ResponseEntity<String> responseEntity =
restTemplate.exchange("https://clamav.my.org/scan", HttpMethod.POST, requestEntity, String.class);
String body = (String) responseEntity.getBody();
这是日志输出
Created POST request for "https://clamav.my.org/scan" Setting request Accept header to [text/plain, application/json, application/*+json, */*] Writing [{name=[fileUpload.obj], file=[[B@fe97ef91]}] as "multipart/form-data" using [org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@4885d075] POST request for "https://clamav.my.org/scan" resulted in 400 (Bad Request); invoking error handler Created POST request for "https://clamav.my.org/scan" Setting request Accept header to [text/plain, application/json, application/*+json, */*] Writing [{file=[[B@fe97ef91]}] using [org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@e8e8b3fa] POST request for "https://clamav.my.org/scan" resulted in 400 (Bad Request); invoking error handler
我已经尝试了几次迭代并搜索了示例代码。关于如何成功请求的任何想法? TIA
在相关的 post、sending-file-over-spring-rest-service-via-resttemplate
中找到了答案现在调用 RestTemplate,
RestTemplate restTemplate = new RestTemplate();
FormHttpMessageConverter converter = new FormHttpMessageConverter();
restTemplate.getMessageConverters().add(converter);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultipartByteArrayResource resource = new MultipartByteArrayResource(in);
resource.setFilename("fileUpload.obj");
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("name", "fileUpload.obj");
parts.add("file", resource);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(parts,
headers);
ResponseEntity<String> responseEntity = restTemplate.exchange("https://clamav.my.org/scan",
HttpMethod.POST, requestEntity, String.class);
String body = (String) responseEntity.getBody();
我相信这可以改进,但它目前正在工作。