Springboot 接口对接文件及对象的操作方法
两个sprongboot项目实现文件对接,在传入文件同时传递其他对象信息,比如接口如下

一、创建文件
例如在D盘下创建1.txt,里边写入内容

1、传送方代码实现
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.File;
/**
* Created by HJ
*/
@RestController
@RequestMapping("/send")
public class test {
@GetMapping("/sendFile")
public ResponseEntity<String> sendFile( ){
//接口地址
String remote_url="http://localhost:8081/receiv/receivFile";
File file=new File("D:\\1.txt");
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(new File("D:\\1.txt")));
Student student= new Student(1,"张三",12);
body.add("student",student);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> response = restTemplate.exchange(remote_url, HttpMethod.POST, requestEntity, String.class);
return response;
}
static class Student {
private int id;
private String name;
private int age;
public Student(int id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
}
public Student(){
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
@Override
public String toString(){
return id+" "+name+" "+age;
}
public void setId(int id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
} }
}2.接收方代码实现
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* Created by HJ
*/
@RestController
@RequestMapping("/receiv")
public class testb {
@RequestMapping(value = "/receivFile", method = RequestMethod.POST)
public String receivFile(@RequestPart("file") MultipartFile file,
@RequestPart("student") Student student) throws IOException {
byte[] bytes = file.getBytes();
String s = new String(bytes);
//InputStream inputStream=file.getInputStream();
System.out.println("文件内容为:"+s);
System.out.println("文件名称:"+file.getOriginalFilename());
System.out.println("对象内容:"+student.toString());
return "对接成功";
}
static class Student {
private int id;
private String name;
private int age;
public Student(int id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
}
public Student(){
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
@Override
public String toString(){
return "id:"+id+" name: "+name+" age:"+age;
}
public void setId(int id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
} }
}3、测试
界面输入传送方项目路径,比如:http://localhost:8082/send/sendFile
界面返回信息

接收方控制台输出

到此这篇关于Springboot 接口对接文件及对象的操作方法的文章就介绍到这了,更多相关Springboot 接口对接内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
关于Spring中的@Configuration中的proxyBeanMethods属性
这篇文章主要介绍了关于Spring中的@Configuration中的proxyBeanMethods属性,需要的朋友可以参考下2023-07-07
Java数据库连接池之DBCP浅析_动力节点Java学院整理
这篇文章主要为大家详细介绍了Java数据库连接池之DBCP的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-08-08
Spring使用Configuration注解管理bean的方式详解
在Spring的世界里,Configuration注解就像是一位细心的园丁,它的主要职责是在这个繁花似锦的园子里,帮助我们声明和管理各种各样的bean,本文给大家介绍了在Spring中如何优雅地管理你的bean,需要的朋友可以参考下2024-05-05
使用mybatis-plus的insert方法遇到的问题及解决方法(添加时id值不存在异常)
这篇文章主要介绍了使用mybatis-plus的insert方法遇到的问题及解决方法(添加时id值不存在异常),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-08-08
基于SpringBoot核心原理(自动配置、事件驱动、Condition)
这篇文章主要介绍了基于SpringBoot核心原理(自动配置、事件驱动、Condition),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-08-08
Java利用Request请求如何获取IP地址对应的省份、城市详解
之前已经给大家介绍了关于Java用Request请求获取IP地址的相关内容,那么下面这篇文章将给大家进入深入的介绍,关于Java利用Request请求如何获取IP地址对应省份、城市的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。2017-10-10
SpringCloud Gateway实现请求解密和响应加密的过程解析
这篇文章主要介绍了SpringCloud Gateway实现请求解密和响应加密的相关知识,本文环境使用比较新的 Java 17 和 SpringBoot 3.1.5,对应到Spring的版本是 6.0.13,本文重心是网关项目,需要的朋友可以参考下2023-11-11


最新评论