springBoot 上传文件传restTemplate 传递MultipartFile

本文介绍了如何在JavaWeb应用中通过`@PostMapping`接收`MultipartFile`类型的文件,处理大文件分块上传,并使用`MultiValueMap`和`FileSystemResource`进行封装,最后通过`RestTemplate`发送HTTP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、第三接口接收File对象文件

2、接收MultipartFile  本地存储

 @PostMapping(value = "/chunkUpload", produces = "application/json")
    public Object chunkUpload(HttpServletRequest request, MultipartFile file) throws IOException {
        String paramJson = HttpRequestUtils.getParam(request);
        ReqChunkUploadDto dto = JSON.parseObject(paramJson,ReqChunkUploadDto.class);
        if(dto.getIdentifier() == null){
            return Result.fail(BizCode.WRONGFUL_PARAM.code(), "文件上传上传凭证不能为空");
        }
        File f = null;
        try {
            String[] fileNames = dto.getFileName().split("\\.");
            if(fileNames.length < 2){
                return null;
            }
            // 创建临时文件
            f = File.createTempFile("jg-", "."+fileNames[fileNames.length-1]);
            file.transferTo(f);
        }catch (Exception e){
            log.error("文件上传异常",e);
        }
        dto.setFile(f);
        return Result.success(bimFileUploadService.chunkUpload(dto));
    }

3、通过MultiValueMap和 FileSystemResource去包装

MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
FileSystemResource resource = new FileSystemResource(dto.getFile());
params.add("file",resource);
HttpEntity requestEntity = new HttpEntity<>(params, headers)
restTemplate.exchange(url, method, requestEntity, String.class);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值