SPRING - REST 模板 - 使用文件输入流和其他原始数据上传文件

SPRING - REST Template - Upload File using File Input Stream & Other primitve data

我知道这已被讨论过很多次,我在 Google / Whosebug 上搜索了很多,但似乎无法正常工作。

我有一个 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 Class:

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);
}

现在,当我 运行 我的应用程序时,我收到以下错误:

Caused by: java.lang.UnsupportedOperationException: getBody not supported at org.springframework.http.client.HttpComponentsStreamingClientHttpRequest.getBodyInternal(HttpComponentsStreamingClientHttpRequest.java:84) at org.springframework.http.client.AbstractClientHttpRequest.getBody(AbstractClientHttpRequest.java:47) at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:299) at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:238) at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:87) at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:777) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:566) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:447)

请让我知道我缺少什么,我无法弄清楚问题所在。

如果需要更多细节,我可以分享

谢谢!!

使用 Spring Web 中的 MultipartFile,然后只需使用 @RequestParam("file") MultipartFile file 作为控制器中的参数。然后您将可以访问流以及上传附带的所有其他属性。

看来我已经解决了。该问题是由于 Spring 4.1.1 版本上的错误造成的。它在 4.2.0 中得到解决 将 pom 依赖项更改为使用 4.2.0,现在可以正常工作了。