【问题标题】:MultipartFile is null when sending a file with Rest template使用 Rest 模板发送文件时 MultipartFile 为空
【发布时间】:2016-09-06 12:28:30
【问题描述】:

您好,我有 2 个独立的应用程序。

第一个是发送 post 请求: 我正在尝试将文件发送到第二个应用程序

public static String httpPostMultipartFile(String url, File file) throws IOException{
    RestTemplate restTemplate = new RestTemplate();
    MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
    parameters.add("file", file); // load file into parameter
    HttpHeaders headers1 = new HttpHeaders();
    headers1.setContentType(MediaType.MULTIPART_FORM_DATA);
    String result = restTemplate.exchange(
            url,
            HttpMethod.POST,
            new HttpEntity<MultiValueMap<String, Object>>(parameters, headers1),
            String.class
        ).getBody();
    return result;

这是第二个收到发布请求的应用

@RequestMapping(value = "/fileupload/save", method = RequestMethod.POST, produces = "text/plain")
@ResponseBody
public String save(@RequestParam(value = "file", required = false) MultipartFile file, @RequestParam(required = false) Boolean suggestTranspose, @RequestParam(value = "transformers", required = false) String transformersString, @RequestParam(required = false) Integer brandId) throws Exception {
.....
}

但在我的第二个应用程序中,MultipartFile 在其余调用中实际上为空

我找到了这个对我有帮助的链接 link 1

link 2

【问题讨论】:

    标签: java spring spring-mvc multipartform-data resttemplate


    【解决方案1】:

    您的 httpPostMultipartFile 方法应如下所示。您需要使用new FileSystemResource(file.getPath()) 添加文件。

    public static String httpPostMultipartFile(String url, File file) throws IOException{
       RestTemplate restTemplate = new RestTemplate();
            MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<String, Object>();     
            multipartMap.add("file", new FileSystemResource(file.getPath()));
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            HttpEntity<Object> request = new HttpEntity<Object>(multipartMap, headers);         
            String result = restTemplate.exchange(
                    url,
                    HttpMethod.POST,
                    request,
                    String.class
                ).getBody();
    
            return result;
    }
    

    【讨论】:

    • 如果您解释了更改的内容和原因,这将是一个更好的答案。
    【解决方案2】:

    这是一个示例(由我测试),说明如何使用 RestTemplate 调用包含文件作为 @RequestParam 的 Web 服务:

    import java.io.File;
    import java.io.IOException;
    
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.MediaType;
    import org.springframework.util.LinkedMultiValueMap;
    import org.springframework.web.client.RestTemplate;
    
    public class Application {
        public static void main(String[] args) throws IOException {
            LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
            FileSystemResource value = new FileSystemResource(new File("src/myFile.txt"));
            map.add("file", value);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.exchange("http://localhost/api/ws/myws", HttpMethod.POST, requestEntity, String.class);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 2019-06-24
      • 2019-04-17
      • 2013-07-31
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多