Springboot 接口对接文件及对象

这篇具有很好参考价值的文章主要介绍了Springboot 接口对接文件及对象。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

两个sprongboot项目实现文件对接,在传入文件同时传递其他对象信息,比如接口如下

Springboot 接口对接文件及对象,# SpringBoot,spring boot,java,spring

一、创建文件

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

Springboot 接口对接文件及对象,# SpringBoot,spring boot,java,spring

 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 
 */
@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;
        }  }

}

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
 */
@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;
        }  }
}

4、测试

界面输入传送方项目路径,比如:http://localhost:8082/send/sendFile

界面返回信息 

 Springboot 接口对接文件及对象,# SpringBoot,spring boot,java,spring

 接收方控制台输出

Springboot 接口对接文件及对象,# SpringBoot,spring boot,java,spring文章来源地址https://www.toymoban.com/news/detail-521805.html

到了这里,关于Springboot 接口对接文件及对象的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 对接第三方接口鉴权(Spring Boot+Aop+注解实现Api接口签名验证)

    一个web系统,从接口的使用范围也可以分为对内和对外两种,对内的接口主要限于一些我们内部系统的调用,多是通过内网进行调用,往往不用考虑太复杂的鉴权操作。但是,对于对外的接口,我们就不得不重视这个问题,外部接口没有做鉴权的操作就直接发布到互联网,而

    2024年04月29日
    浏览(62)
  • SpringBoot+jasypt-spring-boot-starter实现配置文件明文加密

    springboot:2.1.4.RELEASE JDK:8 jasypt-spring-boot-starter:3.0.2 Jasypt默认算法为PBEWithMD5AndDES,该算法需要一个加密密钥,可以在应用启动时指定(环境变量)。也可以直接写入配置文件 3.1 application.properties配置文件版 加密后,可删除jasypt.encryptor.password配置;发版时可在命令行中配置 3.2 函数

    2024年02月15日
    浏览(28)
  • Spring Boot入门(04):SpringBoot实现多环境配置文件切换 | 超级详细,建议收藏

            在开发和部署Spring Boot应用的过程中,经常需要在不同的环境中进行配置,比如开发环境、测试环境、生产环境等。为了方便管理和部署,我们需要实现多环境配置文件切换。本篇教程将带你轻松搞定不同环境部署问题,让你的应用在各个环境中稳定运行。无论你是

    2024年02月12日
    浏览(33)
  • 【Vue】Vue对接SpringBoot接口完整代码

    在Vue中调用SpringBoot接口需要先建立Vue项目,并添加axios库用于发起请求。然后在Vue中编写前端页面,调用SpringBoot接口。 以下是一个示例代码,前端页面需要调用后端接口,实现通过Vue显示SpringBoot后端数据。 在Vue中安装axios库: Vue中编写前端页面代码: 在SpringBoot中编写接口代

    2024年02月08日
    浏览(34)
  • springboot 发送邮件,以及邮件工具类 并且解决spring-boot-starter-mail 发送邮件附件乱码或者文件错乱

    1、设置系统值 System.setProperty(“mail.mime.splitlongparameters”, “false”); 2、 在创建对象的时候定义编码格式(utf-8): MimeMessageHelper helper = new MimeMessageHelper(mes, true, “utf-8”); 3、 其次,在添加附件的时候,附件名是需要定义编码的 helper.addAttachment(MimeUtility.encodeWord(附件名,“utf-8”

    2024年02月15日
    浏览(45)
  • springboot + websocket对接文心一言接口实现简单上下文聊天(贴代码)

    如题,第一次用websocket,做了个这玩意,只做了上下文的聊天,没做流式。 中间还有个低级报错但卡了好久,具体可以看【错误记录】websocket连接失败,但后端毫无反应,还有【错误记录】ruoyi-vue@Autowired注入自定义mapper时为null解决 ,感兴趣可前往观看。 实际上我后端用的

    2024年02月07日
    浏览(38)
  • springboot 对接 minio 分布式文件系统

    1. minio介绍 Minio 是一个基于Go语言的对象存储服务。它实现了大部分亚马逊S3云存储服务接口,可以看做是是S3的开源版本,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大

    2024年02月14日
    浏览(32)
  • SpringBoot对接阿里云OSS上传文件以及回调(有坑)

    今天在对接阿里云OSS对象存储, 把这过程记录下来 阿里云的内容很多,文档是真的难找又难懂 本文主要是用的PostObject API 加上 Callback参数 PostObject - https://help.aliyun.com/document_detail/31988.html?spm=a2c4g.31989.0.0 Callback - https://help.aliyun.com/document_detail/31989.html?spm=a2c4g.31988.0.0 前端向后

    2024年02月11日
    浏览(44)
  • 【Spring Boot】SpringBoot 单元测试

    单元测试(unit testing),是指对软件中的最⼩可测试单元进⾏检查和验证的过程就叫单元测试。 1、可以⾮常简单、直观、快速的测试某⼀个功能是否正确。 2、使⽤单元测试可以帮我们在打包的时候,发现⼀些问题,因为在打包之前,所以的单元测试必须通过,否则不能打包

    2024年02月07日
    浏览(45)
  • SpringBoot整理-Spring Boot配置

    Spring Boot 的配置系统是其核心功能之一,旨在简化 Spring 应用的配置过程。Spring Boot 提供了一种灵活的方式来配置你的应用,无论是通过外部配置文件,环境变量,命令行参数还是在代码中直接配置。以下是关于 Spring Boot 配置的几个重要方面: 配置文件 application.prop

    2024年01月25日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包