当前位置: 首页 > news >正文

【日常记录-Java】SpringBoot将文件上传到另外一个服务

Author:赵志乾
Date:2024-08-22
Declaration:All Right Reserved!!!

1. 引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. application.yml增加配置

upload:url: http://localhost:8080/api/upload

3. 生成RestTemplate

@Configuration
public class BeanConfig {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}
}

4. 上传服务

@Slf4j
@Service
public class FileUploadService implements IFileUploadService {@Resourceprivate RestTemplate restTemplate;@Value("${upload.url}")private String uploadUrl;@Overridepublic boolean uploadExcel(File file, String taskId, String sign) {HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.MULTIPART_FORM_DATA);FileSystemResource resource = new FileSystemResource(file);MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();body.add("file", resource);body.add("taskId", taskId);body.add("sign", sign);HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);ResponseEntity<Boolean> response = restTemplate.postForEntity(uploadUrl, request, Boolean.class);if (!response.getStatusCode().equals(HttpStatus.OK)) {return false;}return response.getBody();}
}


http://www.mrgr.cn/news/10382.html

相关文章:

  • 以简单的例子从头开始建spring boot web多模块项目(二)-mybatis简单集成
  • 010、架构_配置文件_管理节点(clustermanager.ini)
  • Nginx源码安装与进阶负载均衡
  • RK3588开发笔记-pdm接口ES7201音频采集调试记录
  • pygame—炸弹牌(可做课设)
  • 在Ubuntu 18.04上安装Linux、Nginx、MySQL、PHP(LEMP堆栈)的方法
  • Android笔试面试题AI答之Kotlin(20)
  • 【C#】【EXCEL】Bumblebee/Classes/ExData.cs
  • 【Eureka】Eureka 介绍与实战
  • 集合及数据结构第八节(下)———— 队列(Queue)、队列的模拟实现和练习
  • npm install` 报错
  • kafka发送消息-生产者发送消息的分区策略(消息发送到哪个分区中?是什么策略)
  • Linux的NFS共享存储服务(配置案例)
  • 程序运行期发生程序运行期发生ClassNotFoundException
  • [FSCTF 2023]ez_php2
  • 没有人会窃取你的想法,关键在于执行
  • 力扣230题详解:二叉搜索树中第K小的元素的多种解法与模拟面试问答
  • 如何解决git@github.com: Permission denied (publickey)异常
  • MinerU 是一款将PDF转化如markdown、json工具
  • 如何使用Python自动化测试工具Selenium进行网页自动化?