Springboot 接口对接文件和对象
两个sprongboot项目实现文件对接,在传入文件同时传递其他对象信息,比如接口如下
一、创建文件
例如在D盘下创建1.txt,里边写入内容
2、传送方代码实现
-
-
-
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
-
*/
-
-
"/send")(
-
public class test {
-
-
"/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;
-
}
-
-
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;
-
} }
-
-
}
3.接收方代码实现
-
-
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
-
*/
-
-
-
public class testb {
-
-
public String receivFile( MultipartFile file,
-
throws IOException {Student student)
-
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;
-
}
-
-
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;
-
} }
-
}
4、测试
界面输入传送方项目路径,比如:http://localhost:8082/send/sendFile
界面返回信息
接收方控制台输出
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgaaeba
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13