zoukankan      html  css  js  c++  java
  • 使用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较为简单。

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

  • 相关阅读:
    Mysql 执行安装脚本报错Changed limits:
    Centos6.6 安装Mysql集群
    Oracle11g RAC+DG搭建
    Oracle用函数或PIVOT实现行转列
    Oracle根据列中的特殊符号进行分组
    Hadoop on Windows with Eclipse -02- Prerequisites
    Hadoop on Windows with Eclipse -01- Introduction
    Hadoop入门之WordCount运行详解
    Hadoop namenode无法启动问题解决
    jar 打包命令详解
  • 原文地址:https://www.cnblogs.com/super-chao/p/15338098.html
Copyright © 2011-2022 走看看