Spring Cloud RestTemplate 单文件上传、多文件上传

41 篇文章 0 订阅
13 篇文章 0 订阅

单文件上传:

服务提供者

controller

 @RequestMapping(value = "/addProject",method = RequestMethod.POST)
    public AjaxResult addSave( HjProject hjProject, MultipartFile file)throws Exception
    {
        if (file != null&&!file.isEmpty()) {
            //上传图片到oss服务器
            String url = AliyunOSSClientUtil.uploadFileImg(file, "file", hjProject.getShortName() + System.currentTimeMillis() + ".jpg");
            hjProject.setProjectImage(url.substring(0,url.indexOf("?")));
        }


        int i = hjProjectService.insertHjProject(hjProject);
        if(i>0){
            HjCompanyProject hjCompanyProject = new HjCompanyProject();
            hjCompanyProject.setCompanyId(cid);
            hjCompanyProject.setProjectId(hjProject.getId());
            return toAjax(hjCompanyProjectService.insertHjCompanyProject(hjCompanyProject));
        }
        return toAjax(i);
    }

消费者

RestTemplate

@Configuration
public class RestConfiguration {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
    
}

controller

 @Autowired
    private RestTemplate restTemplate;

@RequestMapping("/addProject")
    public AjaxResult addSave(HjProject hjProject, MultipartFile file)throws Exception
    {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        //生成临时文件
        File localFile = new File(getPath(),"/"+file.getOriginalFilename());
        file.transferTo(localFile);

        FileSystemResource resource = new FileSystemResource(localFile);
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("file", resource);//文件
 
        for (Field f : hjProject.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            //遍历对象属性:值
            param.add(f.getName(), f.get(hjProject));

        }


        //用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(param, headers);
        //请求服务
        AjaxResult s = restTemplate.postForObject("http://服务名称/provider/project/addProject", files, AjaxResult.class);
        System.out.println(s);
        //删除临时文件
        localFile.delete();
        return s;
    }

 

多文件上传:

服务消费者

controller

public AjaxResult upload(MultipartFile[] file)throws Exception{
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);

        File[] files = new File[file.length];
        //遍历文件数组
        for(int i=0;i<file.length;i++){

            File localFile = new File(Util.getPath(),"/"+file[i].getOriginalFilename());
            file[i].transferTo(localFile);
            files[i]=localFile;
            //多个MultipartFile同时添加进 MultiValueMap  key为file
            //MultiValueMap可以让一个key对应多个value
            param.add("file", new FileSystemResource(localFile));
        }

        
        //用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
        AjaxResult reult = restTemplate.postForObject("http://服务名称/provider/fileApi/upload", httpEntity, AjaxResult.class);
        //删除临时文件
        for(int i=0;i<files.length;i++){
            if(files[i].exists()){
                System.out.println("删除临时文件"+files[i].getName());
                files[i].delete();
            }

        }
        return reult;
    }

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云下的你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值