`
lfwer
  • 浏览: 84203 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

restTemplate 上传文件

阅读更多

1 添加依赖:

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

 

2 注入Bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory clientHttpRequestFactory() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(30000);
        httpRequestFactory.setConnectTimeout(30000);
        httpRequestFactory.setReadTimeout(30000);
        return httpRequestFactory;
    }

}

 

3 上传文件

@Autowired
private RestTemplate restTemplate;

public void upload(){
    File file = new File("xx");
    FileSystemResource resource = new FileSystemResource(file);
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
    param.add("file", resource);
    String result = restTemplate.postForObject("xx", param, String.class);
}

 

分享到:
评论

相关推荐

    精讲RestTemplate文件上传下载与大文件流式下载.docx

    ResponseEntity[]&gt; rsp = restTemplate.getForEntity(url, byte[].class); System.out.println("文件下载请求结果状态码:" + rsp.getStatusCode()); // 将下载下来的文件内容保存到本地 String targetPath = ...

    FileUpload.zip

    使用restTemplate上传文件,分为两部分,客户端、服务端,对应文章链接,https://blog.csdn.net/sutongxuevip/article/details/105660030。

    springboot.zip

    使用restTemplate上传文件,分为两部分,客户端、服务端,对应文章链接,https://blog.csdn.net/sutongxuevip/article/details/105660030。

    文件流上传 下载

    文件流,其实我最讨厌的,但是觉得有用就拿来让大家看看,共享,一定要共享啊,免费的,咱是穷人啊!

    download:也可以处理大文件下载的 Ruby 下载助手

    Ruby 下载助手,甚至可以处理大文件下载 用例 少授权用例 require 'download' Download . file &#40; uri_path , target_local_file_path &#41; 具有基本身份验证 Download . file &#40; '...

    接口开发、springboot、接口转发、前端直接调用图床API时我们发现会报错,编写一个后端接口进行代理即可解决,已实现的例子

    以下是一个基于`RestTemplate`转发文件上传请求到目标服务的示例 主要运用了以下技术: 1. Spring MVC框架 构建Web应用程序 2. Apache HttpClient库 模拟请求API接口 3. MultipartEntityBuilder 将上传相关参数以...

    基于SpringCloud开发的在线教育系统

    在线教育平台服务端基于Spring Boot构建,采用Spring Cloud微服务框架。 持久层:MySQL、MongoDB、Redis、ElasticSearch 数据访问层:使用Spring Data JPA 、Mybatis、Spring Data ...4 文件上传微服务 5 网关微服务

    SpringBootApplications

    该文件夹将用于保存上载文件的内容。 src / main / resources / DownloadRepository:Download API将在此处写入文件内容。 我使用RestTemplate进行客户端HTTP访问。 将Application.java作为Spring Boot Application...

    SpringBootLearning_forezp.tar.gz

    SpringBoot非官方教程 | 第十七篇:上传文件 任务调度 SpringBoot非官方教程 | 第十八篇: 定时任务(Scheduling Tasks) 表单相关 SpringBoot非官方教程 | 第十九篇: 验证表单信息 SpringBoot非官方教程 | 第二...

    spring-boot:学习Spring Boot

    1.Web开发:使用RestTemplate解析RESTful服务:文件上传,下载:发送邮件:在线API文档:API文档生成:应用监控2.数据访问:使用 MyBatis 连接数据库:整合 Druid 作数据库连接池:使用 Jpa 连接数据库:使用 Redis ...

    奇安信安全扫描漏洞解决路径遍历和存储型xss和反射xss攻击漏洞

    亲测可用,中级漏洞,解决服务器段请求伪造 final String forURL = ESAPI.encoder().encodeForURL(url);...restTemplate.exchange(forURL, HttpMethod.GET,new HttpEntity&lt;String&gt;(headers), List.class, params);

    Spring.3.x企业应用开发实战(完整版).part2

    15.6.2 编写控制器和文件上传表单页面 15.7 杂项 15.7.1 静态资源处理 15.7.2 装配拦截器 15.7.3 异常处理 15.8 小结 第5篇 测试及实战 第16章 实战型单元测试 16.1 单元测试概述 16.1.1 为什么需要单元测试 16.1.2...

    Spring3.x企业应用开发实战(完整版) part1

    15.6.2 编写控制器和文件上传表单页面 15.7 杂项 15.7.1 静态资源处理 15.7.2 装配拦截器 15.7.3 异常处理 15.8 小结 第5篇 测试及实战 第16章 实战型单元测试 16.1 单元测试概述 16.1.1 为什么需要单元测试 16.1.2...

Global site tag (gtag.js) - Google Analytics