最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot 接口對接文件及對象的操作方法

 更新時間:2023年07月05日 14:35:30   作者:心寒丶  
這篇文章主要介紹了Springboot 接口對接文件及對象的操作,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

兩個sprongboot項目實現(xiàn)文件對接,在傳入文件同時傳遞其他對象信息,比如接口如下

一、創(chuàng)建文件

例如在D盤下創(chuàng)建1.txt,里邊寫入內(nèi)容

1、傳送方代碼實現(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.接收方代碼實現(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("對象內(nèi)容:"+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 接口對接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java 實現(xiàn)DES 加密解密的示例

    java 實現(xiàn)DES 加密解密的示例

    這篇文章主要介紹了java 實現(xiàn)DES 加密解密的示例代碼,幫助大家更好的理解和使用Java進行加解密,感興趣的朋友可以了解下
    2020-12-12
  • SpringMVC使用第三方組件實現(xiàn)文件上傳

    SpringMVC使用第三方組件實現(xiàn)文件上傳

    這篇文章主要介紹了SpringMVC使用第三方組件實現(xiàn)文件上傳,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • JPA之映射mysql text類型的問題

    JPA之映射mysql text類型的問題

    這篇文章主要介紹了JPA之映射mysql text類型的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot如何監(jiān)控Redis中某個Key的變化(自定義監(jiān)聽器)

    SpringBoot如何監(jiān)控Redis中某個Key的變化(自定義監(jiān)聽器)

    這篇文章主要介紹了SpringBoot如何監(jiān)控Redis中某個Key的變化(自定義監(jiān)聽器),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 淺談@FeignClient中name和value屬性的區(qū)別

    淺談@FeignClient中name和value屬性的區(qū)別

    這篇文章主要介紹了@FeignClient中name和value屬性的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • IDEA如何解決properties文件亂碼問題

    IDEA如何解決properties文件亂碼問題

    IDEA編輯器中打開properties文件時出現(xiàn)中文亂碼,可以通過修改文件編碼格式來解決,具體步驟為:設置》Setting》Editor》FileEncodings,將編碼格式更改為UTF-8
    2025-01-01
  • Spring?AOPr如何打通兩個切面之間的通信

    Spring?AOPr如何打通兩個切面之間的通信

    本文主要介紹了Spring?AOPr如何打通兩個切面之間的通信,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-03-03
  • Java語言實現(xiàn)最大堆代碼示例

    Java語言實現(xiàn)最大堆代碼示例

    這篇文章主要介紹了Java語言實現(xiàn)最大堆代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-12-12
  • Java中的StringTokenizer實現(xiàn)字符串切割詳解

    Java中的StringTokenizer實現(xiàn)字符串切割詳解

    這篇文章主要介紹了Java中的StringTokenizer實現(xiàn)字符串切割詳解,java.util工具包提供了字符串切割的工具類StringTokenizer,Spring等常見框架的字符串工具類(如Spring的StringUtils),需要的朋友可以參考下
    2024-01-01
  • 常用Maven庫,鏡像庫及maven/gradle配置(小結(jié))

    常用Maven庫,鏡像庫及maven/gradle配置(小結(jié))

    這篇文章主要介紹了常用Maven庫,鏡像庫及maven/gradle配置(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12

最新評論

漳浦县| 元江| 井陉县| 鲁山县| 游戏| 泾川县| 鄂州市| 孙吴县| 剑阁县| 神农架林区| 张北县| 内江市| 怀来县| 蛟河市| 潜江市| 左云县| 闽清县| 罗田县| 耿马| 泗阳县| 沂南县| 北海市| 桐城市| 西畴县| 东丽区| 屏东市| 南通市| 宿迁市| 莱州市| 海阳市| 新干县| 靖宇县| 丹阳市| 乌拉特前旗| 姜堰市| 许昌市| 东港市| 永宁县| 新津县| 宿松县| 大厂|