@Test
public void testUpload() throws Exception {
String url = "http://127.0.0.1/file/upload";
String filePath = "C:\\temp\\1.png"; RestTemplate rest = new RestTemplate();
FileSystemResource resource = new FileSystemResource(new File(filePath));
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
param.add("file", resource);
param.add("fid", "1"); String string = rest.postForObject(url, param, String.class); System.out.println(string);
}

或者

    @Test
public void testUpload2() throws Exception { String url = "http://127.0.0.1/file/upload";
String filePath = "C:\\temp\\1.png"; RestTemplate rest = new RestTemplate();
FileSystemResource resource = new FileSystemResource(new File(filePath));
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
param.add("file", resource);
param.add("fid", "1"); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param);
ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class); System.out.println(responseEntity.getBody()); }

SpringBoot项目 前后端分离 ajax附件上传下载,参考SpringBoot项目 前后端分离 ajax附件上传下载

SpringBoot RestTemplate 上传文件的更多相关文章

  1. Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件

    springboot部署之后无法获取项目目录的问题: 之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar 运行 ...

  2. 通过`RestTemplate`上传文件(InputStreamResource详解)

    通过RestTemplate上传文件 1.上传文件File 碰到一个需求,在代码中通过HTTP方式做一个验证的请求,请求的参数包含了文件类型.想想其实很简单,直接使用定义好的MultiValueMap ...

  3. SpringBoot初探(上传文件)

    学了Spring,SpringMVC,Mybatis这一套再来看SpringBoot,心里只有一句握草,好方便 这里对今天做的东西做个总结,然后在这之间先安利一个热部署的工具,叫spring-DevT ...

  4. Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法

    记录瞬间 近段时间使用Springboot实现了文件的上传服务,但是在使用python的requests进行post上传时,总是报错. 比如: 1.Current request is not a m ...

  5. RestTemplate上传文件

    1.上传的文件是File类型 如果文件保存在本地,即可以通过File file = new File(path) 或者 文件路径地址获取到指定文件 public String uploadFile(F ...

  6. springboot项目上传文件出现临时文件目录为空

    最近写文件上传到服务器读取的代码,前端使用FormData上传,服务端用MultipartFile接收,自己测试了下MultipartFile对象有什么东西,结果一般属性都能出来,测试getInput ...

  7. 使用RestTemplate上传文件给远程接口

    MultiValueMap request = new LinkedMultiValueMap(1); ByteArrayResource is = new ByteArrayResource(mul ...

  8. springboot +fastdfs 上传文件到到云服务器

    fastdfs在云服务器的搭建和配置:https://blog.csdn.net/qq_41592652/article/details/104006289 springboot结构如下: appli ...

  9. 二、SpringBoot实现上传文件到fastDFS文件服务器

    上篇文章介绍了如何使用docker安装fastDFS文件服务器,这一篇就介绍整合springBoot实现文件上传到fastDFS文件服务器 1.pom.xml文件添加依赖 <!-- 连接fast ...

  10. springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux

    //我的会员中心 头像上传接口 /*windows 调试*/ @Value("${appImg.location}") private String winPathPic; /*l ...

随机推荐

  1. NC14291 Cut

    题目链接 题目 题目描述 给你一个长度为 \(n\) 的序列,你每次可以将一个序列分割成两个连续的的子序列, 分割的代价为原序列的总和. 现在允许你在初始时将序列重新排列一次. 问分割成 \(n\) ...

  2. Ubuntu18.04 Server部署Flannel网络的Kubernetes

    准备服务器 ESXi6.5安装Ubuntu18.04 Server, 使用三台主机, 计划使用hostname为 kube01, kube02, kube03, 配置为2核4G/160G, K8s要求 ...

  3. python中矩阵合并、拼接、组合

    1 numpy数组 1.1 append() import numpy as np a=np.array([[1,3],[5,7]]) b=np.array([[2,4],[6,8]]) c=np.a ...

  4. Context与Reducer

    Context与Reducer Context是React提供的一种跨组件的通信方案,useContext与useReducer是在React 16.8之后提供的Hooks API,我们可以通过use ...

  5. join命令

    join命令 join命令用于将两个文件中,指定栏位内容相同的行连接起来.其首先找出两个文件中指定栏位内容相同的行,并加以合并,再输出到标准输出设备. 语法 join [OPTION]... FILE ...

  6. Js的GC机制

    Js的GC机制 在Js七种基本类型中的引用类型Object的变量其占据内存空间大且大小不固定,在堆内存中实际存储对象,在栈内存中存储对象的指针,对于对象的访问是按引用访问的.在栈区中执行的变量等是通过 ...

  7. python开发接口时,使用jsonschema模块对数据进行校验

    import jsonschema schema = { "type": "object", # 先声明每个键都是对象 "properties&quo ...

  8. 【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known

    问题描述 使用 azure-spring-boot-starter-storage 来上传文件到 Storage Blob中,并把应用部署到Azure 中国区的Spring Cloud服务后,调用上传 ...

  9. [爬坑] termux ssh 设置总是 permission denied

    问题 设置ssh之后,客户端登录会提示 permission denied 的问题,经过排查最终确定是 shell设置错误的问题,解决方法如下 http://new.aidlearning.net/d ...

  10. springboot中使用restTemplate发送带参数和请求头的post,get请求

    最近在工作中使用到了用restTemplate去获取网站数据填入到数据库中,在这里记录下来以便以后使用: 添加相关依赖:版本使用springboot中的 <dependency> < ...