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

时间:2015-10-16 10:35:00

标签: java spring multipartform-data spring-rest

我知道这已经讨论了很多次了,我在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:不支持getBody       在org.springframework.http.client.HttpComponentsStreamingClientHttpRequest.getBodyInternal(HttpComponentsStreamingClientHttpRequest.java:84)       在org.springframework.http.client.AbstractClientHttpRequest.getBody(AbstractClientHttpRequest.java:47)       在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)

请让我知道我错过了什么,我无法弄清楚这个问题。

如果需要进一步的细节,我可以分享

谢谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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