Springboot 接口對(duì)接文件及對(duì)象的操作方法
兩個(gè)sprongboot項(xiàng)目實(shí)現(xiàn)文件對(duì)接,在傳入文件同時(shí)傳遞其他對(duì)象信息,比如接口如下
一、創(chuàng)建文件
例如在D盤下創(chuàng)建1.txt,里邊寫(xiě)入內(nèi)容
1、傳送方代碼實(shí)現(xiàn)
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.接收方代碼實(shí)現(xiàn)
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("文件內(nèi)容為:"+s); System.out.println("文件名稱:"+file.getOriginalFilename()); System.out.println("對(duì)象內(nèi)容:"+student.toString()); return "對(duì)接成功"; } 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、測(cè)試
界面輸入傳送方項(xiàng)目路徑,比如:http://localhost:8082/send/sendFile
界面返回信息
接收方控制臺(tái)輸出
到此這篇關(guān)于Springboot 接口對(duì)接文件及對(duì)象的操作方法的文章就介紹到這了,更多相關(guān)Springboot 接口對(duì)接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性
這篇文章主要介紹了關(guān)于Spring中的@Configuration中的proxyBeanMethods屬性,需要的朋友可以參考下2023-07-07關(guān)于Rabbitmq死信隊(duì)列及延時(shí)隊(duì)列的實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于Rabbitmq死信隊(duì)列及延時(shí)隊(duì)列的實(shí)現(xiàn),TTL就是消息或者隊(duì)列的過(guò)期功能,當(dāng)消息過(guò)期就會(huì)進(jìn)到死信隊(duì)列,死信隊(duì)列和普通隊(duì)列沒(méi)啥區(qū)別,然后我們只需要配置一個(gè)消費(fèi)者來(lái)消費(fèi)死信隊(duì)列里面的消息就可以了,需要的朋友可以參考下2023-08-08Java數(shù)據(jù)庫(kù)連接池之DBCP淺析_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)庫(kù)連接池之DBCP的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Spring使用Configuration注解管理bean的方式詳解
在Spring的世界里,Configuration注解就像是一位細(xì)心的園丁,它的主要職責(zé)是在這個(gè)繁花似錦的園子里,幫助我們聲明和管理各種各樣的bean,本文給大家介紹了在Spring中如何優(yōu)雅地管理你的bean,需要的朋友可以參考下2024-05-05使用mybatis-plus的insert方法遇到的問(wèn)題及解決方法(添加時(shí)id值不存在異常)
這篇文章主要介紹了使用mybatis-plus的insert方法遇到的問(wèn)題及解決方法(添加時(shí)id值不存在異常),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08基于SpringBoot核心原理(自動(dòng)配置、事件驅(qū)動(dòng)、Condition)
這篇文章主要介紹了基于SpringBoot核心原理(自動(dòng)配置、事件驅(qū)動(dòng)、Condition),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Java利用Request請(qǐng)求如何獲取IP地址對(duì)應(yīng)的省份、城市詳解
之前已經(jīng)給大家介紹了關(guān)于Java用Request請(qǐng)求獲取IP地址的相關(guān)內(nèi)容,那么下面這篇文章將給大家進(jìn)入深入的介紹,關(guān)于Java利用Request請(qǐng)求如何獲取IP地址對(duì)應(yīng)省份、城市的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10SpringCloud?Gateway實(shí)現(xiàn)請(qǐng)求解密和響應(yīng)加密的過(guò)程解析
這篇文章主要介紹了SpringCloud?Gateway實(shí)現(xiàn)請(qǐng)求解密和響應(yīng)加密的相關(guān)知識(shí),本文環(huán)境使用比較新的?Java?17?和?SpringBoot?3.1.5,對(duì)應(yīng)到Spring的版本是?6.0.13,本文重心是網(wǎng)關(guān)項(xiàng)目,需要的朋友可以參考下2023-11-11MyBatis自定義類型轉(zhuǎn)換器實(shí)現(xiàn)加解密
這篇文章主要介紹了MyBatis自定義類型轉(zhuǎn)換器實(shí)現(xiàn)加解密的相關(guān)資料,需要的朋友可以參考下2016-07-07JavaWeb項(xiàng)目音頻資源播放實(shí)現(xiàn)方法詳解
這篇文章主要介紹了JavaWeb項(xiàng)目音頻資源播放實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10