使用RestTemplate调用接口上传文件

参考:

https://www.cnblogs.com/theRhyme/p/10110416.html (这种方式需要创建临时文件)

https://blog.csdn.net/weixin_42615290/article/details/119558150 (这种方式不需要临时文件。但是包装太多,包装了两个HttpEntity和两个HttpHeaders)

https://www.jianshu.com/p/a326a2ffafde(提供了两种方式,方式1需要创建临时文件,方式2不需要创建临时文件)

https://blog.csdn.net/kahhy/article/details/78115698 (这种方式需要创建临时文件)

https://www.jianshu.com/p/e4cc1e293c8f (入参不是MultipartFile而是File类型,如果是MultipartFile需要转换为File类型)

 

上面有的博客需要创建本地临时文件,有的不需要。

其实不需要创建临时文件也可上传:

 方式1:

@PostMapping("/importEquipmentAccounts")
    @ApiOperation(value = "设备台账导入", notes = "liuyachao")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Token", value = "token",  required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "file", value = "需要导入的excel文件", required = true, paramType = "form", dataType = "file")
    })
    public ResponseObjectResult importEquipmentAccounts(@RequestPart(value = "file") MultipartFile file,
                                                        HttpServletResponse resp) {
        File tempFile = null;
        try {
            //设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            HttpHeaders fileHeader= new HttpHeaders();
            fileHeader.setContentType(MediaType.parseMediaType(file.getContentType()));
            fileHeader.setContentDispositionFormData("file", file.getOriginalFilename());
            //设置请求体,注意是LinkedMultiValueMap
            HttpEntity fileEntity = new HttpEntity<>(new ByteArrayResource(file.getBytes()),fileHeader);
            MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
            form.add("file", fileEntity);
            //用HttpEntity封装整个请求报文
            HttpEntity files = new HttpEntity<>(form, headers);
            ResponseEntity<ResponseObjectResult> entity = restTemplate.postForEntity(hosBaseUrl + WebConfig.IMPORT_EQUIPMENT_ACCOUNTS, files, ResponseObjectResult.class);
            ResponseObjectResult responseObjectResult = entity.getBody();
            return responseObjectResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseObjectResult(new ResponseStatus(ResultCode.FAIL));
        }
    }

方式2:

@PostMapping("/importEquipmentAccounts")
    @ApiOperation(value = "设备台账导入", notes = "liuyachao")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Token", value = "token",  required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "file", value = "需要导入的excel文件", required = true, paramType = "form", dataType = "file")
    })
    public ResponseObjectResult importEquipmentAccounts(@RequestPart(value = "file") MultipartFile file,
                                                        HttpServletResponse resp) {
        File tempFile = null;
        try {
            //设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
            form.add("file", file.getResource());
            //用HttpEntity封装整个请求报文
            HttpEntity files = new HttpEntity<>(form, headers);
            ResponseEntity<ResponseObjectResult> entity = restTemplate.postForEntity(hosBaseUrl + WebConfig.IMPORT_EQUIPMENT_ACCOUNTS, files, ResponseObjectResult.class);
            ResponseObjectResult responseObjectResult = entity.getBody();
            return responseObjectResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseObjectResult(new ResponseStatus(ResultCode.FAIL));
        }
    }

方式2较为简单。

 

 

 

 

(这种方式需要创建临时文件)

posted @ 2021-09-26 14:48  super超人  阅读(1096)  评论(0编辑  收藏  举报