• 使用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较为简单。

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

  • 相关阅读:
    yii框架源码分析之Yii::createWebApplication()>run() 执行过程分析
    PHP的范围解析操作符(::)的涵义
    数据结构&算法(PHP描述) 三元组 Triplet
    解决远程连接mysql很慢的问题(mysql_connect 打开连接慢)
    第4章 数据处理数组的处理郑阿奇
    PHP相似函数整理2 array_flip() 、array_reverse()
    OS + Linux Disk disk lvm / disk partition / disk mount / disk io
    my read_Country
    OS + Linux File nfs / samba / rsync / inotify / smb / webdav
    Unix + OS IBM Aix Disk disk lvm / disk partition / disk mount / disk mon / File
  • 原文地址:https://www.cnblogs.com/super-chao/p/15338098.html
Copyright © 2020-2023  润新知