我知道这已经讨论过很多次了,我在 Google / Stackoverflow 上搜索了很多,但似乎无法让它发挥作用。
我有一个 Spring MVC Web 应用程序,JSP 发送媒体文件和一些文本字段,控制器捕获输入流和其他表单数据(一些文本字段),并创建一个 FileUploadRequest 类型的 POJO 对象。完整的数据(文件+文本字段)必须按原样传递给另一个服务(第三方服务)以进行上传。
我正在使用 Spring REST 模板连接到第三方服务。以下是代码片段:
RestTemplate 初始化(Java 配置)
@Bean
public ClientHttpRequestFactory httpRequestFactory() {
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient());
requestFactory.setBufferRequestBody(false);
return requestFactory;
}
@Bean
public RestTemplate getRestTemplate() {
final RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(httpRequestFactory()); // apache http library
restTemplate.setMessageConverters(getMessageConverters());
}
private List<HttpMessageConverter<?>> getMessageConverters() {
final List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
final FormHttpMessageConverter e = new FormHttpMessageConverter();
e.addPartConverter(new MappingJackson2HttpMessageConverter());
converters.add(e);
return converters;
}
@Bean
public CommonsMultipartResolver multipartResolver() {
final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
return commonsMultipartResolver;
}
控制器代码:
public APPResponse uploadFile(final HttpServletRequest request) {
final FileUploadRequest uploadRequest = new FileUploadRequest();
final List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (final FileItem item : items) {
if (item.isFormField()) {
String fieldValue = item.getString();
uploadRequest.setEnabled(Boolean.valueOf(fieldValue));
} else {
String fileName = FilenameUtils.getName(item.getName());
InputStream fileContent = item.getInputStream();
uploadRequest.setFileName(fileName);
uploadRequest.setFileStream(fileContent);
}
}
uploadFileToService(uploadRequest);
}
POJO类:
public class FileUploadRequest {
private String fileName;
private InputStream fileStream;
private boolean enabled;
// getter setters...
}
uploadFileToService 方法实现
public FileUploadResponse uploadFileToService(final FileUploadRequest uploadRequest) {
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
final String url = <URL where the file is to passed as is>;
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
parameters.set("Content-Type","multipart/form-data");
parameters.add("file", uploadRequest.getFileStream());
parameters.add("someOtherParam", uploadRequest.isEnabled());
final HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(
parameters, headers);
final ResponseEntity<T> exchange = template.exchange(url,
HttpMethod.POST, httpEntity, FileUploadResponse.class);
}
现在,当我运行我的应用程序时,我收到以下错误:
引起:java.lang.UnsupportedOperationException:在 org.springframework.http.client.AbstractClientHttpRequest.getBody(AbstractClientHttpRequest.java:47) 的 org.springframework.http.client.HttpComponentsStreamingClientHttpRequest.getBodyInternal(HttpComponentsStreamingClientHttpRequest.java:84) 不支持 getBody在 org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:299) 在 org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:238) 在 org.springframework.http.converter.FormHttpMessageConverter.write (FormHttpMessageConverter.java:87) 在 org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:777) 在 org.springframework.web.client.RestTemplate。doExecute(RestTemplate.java:566) 在 org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529) 在 org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:447)
请让我知道我错过了什么,我无法弄清楚这个问题。
如果需要更多详细信息,我可以分享
谢谢!!