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

 更新时间:2023年07月05日 14:35:30   作者:心寒丶  
这篇文章主要介绍了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 Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)

    Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)

    MyBatis-Plus是MyBatis的增强工具,本文主要介绍了Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL),具有一定的参考价值,感兴趣的可以了解一下
    2021-07-07
  • springboot反爬虫组件kk-anti-reptile的使用方法

    springboot反爬虫组件kk-anti-reptile的使用方法

    这篇文章主要介绍了springboot反爬虫组件kk-anti-reptile的使用方法,帮助大家更好的利用spring boot反爬虫,保护网站安全,感兴趣的朋友可以了解下
    2021-01-01
  • 详解Kotlin中的面向对象(一)

    详解Kotlin中的面向对象(一)

    这篇文章主要介绍了详解Kotlin中的面向对象(一)的相关资料,需要的朋友可以参考下
    2017-06-06
  • 使用Mybatis遇到的there is no getter异常

    使用Mybatis遇到的there is no getter异常

    这篇文章主要介绍了使用Mybatis遇到的there is no getter异常,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 解决RabbitMq消息队列Qos Prefetch消息堵塞问题

    解决RabbitMq消息队列Qos Prefetch消息堵塞问题

    这篇文章主要为大家介绍了关于如何解决解决RabbitMq Qos Prefetch消息堵塞的问题分析,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2022-01-01
  • Spring Boot Admin实践详解

    Spring Boot Admin实践详解

    在本篇文章里小编给大家整理了关于Spring Boot Admin实践的相关知识点,有需要的朋友们可以学习下。
    2019-12-12
  • Java集合框架入门之泛型和包装类

    Java集合框架入门之泛型和包装类

    Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数
    2021-10-10
  • MyBatis中动态SQL语句@Provider的用法

    MyBatis中动态SQL语句@Provider的用法

    本文主要介绍了MyBatis中动态SQL语句@Provider的用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • jackson 如何将实体转json json字符串转实体

    jackson 如何将实体转json json字符串转实体

    这篇文章主要介绍了jackson 实现将实体转json json字符串转实体,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • 解决Maven中关于依赖导入不进的问题

    解决Maven中关于依赖导入不进的问题

    这篇文章主要介绍了解决Maven中关于依赖导入不进的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11

最新评论