• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Springboot 接口对接文件和对象

武飞扬头像
心寒丶
帮助1

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

学新通

一、创建文件

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

学新通

 2、传送方代码实现

  1.  
     
  2.  
     
  3.  
    import org.springframework.core.io.FileSystemResource;
  4.  
    import org.springframework.http.*;
  5.  
    import org.springframework.util.LinkedMultiValueMap;
  6.  
    import org.springframework.util.MultiValueMap;
  7.  
    import org.springframework.web.bind.annotation.GetMapping;
  8.  
    import org.springframework.web.bind.annotation.RequestMapping;
  9.  
    import org.springframework.web.bind.annotation.RestController;
  10.  
    import org.springframework.web.client.RestTemplate;
  11.  
     
  12.  
    import java.io.File;
  13.  
     
  14.  
    /**
  15.  
    * Created by HJ
  16.  
    */
  17.  
    @RestController
  18.  
    @RequestMapping("/send")
  19.  
    public class test {
  20.  
     
  21.  
    @GetMapping("/sendFile")
  22.  
    public ResponseEntity<String> sendFile( ){
  23.  
    //接口地址
  24.  
    String remote_url="http://localhost:8081/receiv/receivFile";
  25.  
    File file=new File("D:\\1.txt");
  26.  
    RestTemplate restTemplate = new RestTemplate();
  27.  
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
  28.  
    body.add("file", new FileSystemResource(new File("D:\\1.txt")));
  29.  
    Student student= new Student(1,"张三",12);
  30.  
    body.add("student",student);
  31.  
    HttpHeaders headers = new HttpHeaders();
  32.  
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  33.  
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
  34.  
    ResponseEntity<String> response = restTemplate.exchange(remote_url, HttpMethod.POST, requestEntity, String.class);
  35.  
     
  36.  
    return response;
  37.  
    }
  38.  
    static class Student {
  39.  
    private int id;
  40.  
    private String name;
  41.  
    private int age;
  42.  
    public Student(int id,String name,int age){
  43.  
    this.id=id;
  44.  
    this.name=name;
  45.  
    this.age=age;
  46.  
    }
  47.  
    public Student(){
  48.  
     
  49.  
    }
  50.  
    public int getId(){
  51.  
    return id;
  52.  
    }
  53.  
    public String getName(){
  54.  
    return name;
  55.  
    }
  56.  
    public int getAge(){
  57.  
    return age;
  58.  
    }
  59.  
    @Override
  60.  
    public String toString(){
  61.  
    return id " " name " " age;
  62.  
    }
  63.  
    public void setId(int id){
  64.  
    this.id=id;
  65.  
    }
  66.  
    public void setName(String name){
  67.  
    this.name=name;
  68.  
    }
  69.  
    public void setAge(int age){
  70.  
    this.age=age;
  71.  
    } }
  72.  
     
  73.  
    }
学新通

3.接收方代码实现

  1.  
     
  2.  
    import org.springframework.web.bind.annotation.RequestMapping;
  3.  
    import org.springframework.web.bind.annotation.RequestMethod;
  4.  
    import org.springframework.web.bind.annotation.RequestPart;
  5.  
    import org.springframework.web.bind.annotation.RestController;
  6.  
    import org.springframework.web.multipart.MultipartFile;
  7.  
     
  8.  
    import java.io.IOException;
  9.  
     
  10.  
    /**
  11.  
    * Created by HJ
  12.  
    */
  13.  
    @RestController
  14.  
    @RequestMapping("/receiv")
  15.  
    public class testb {
  16.  
    @RequestMapping(value = "/receivFile", method = RequestMethod.POST)
  17.  
    public String receivFile(@RequestPart("file") MultipartFile file,
  18.  
    @RequestPart("student") Student student) throws IOException {
  19.  
    byte[] bytes = file.getBytes();
  20.  
    String s = new String(bytes);
  21.  
    //InputStream inputStream=file.getInputStream();
  22.  
    System.out.println("文件内容为:" s);
  23.  
    System.out.println("文件名称:" file.getOriginalFilename());
  24.  
    System.out.println("对象内容:" student.toString());
  25.  
    return "对接成功";
  26.  
     
  27.  
    }
  28.  
    static class Student {
  29.  
    private int id;
  30.  
    private String name;
  31.  
    private int age;
  32.  
    public Student(int id,String name,int age){
  33.  
    this.id=id;
  34.  
    this.name=name;
  35.  
    this.age=age;
  36.  
    }
  37.  
    public Student(){
  38.  
     
  39.  
    }
  40.  
    public int getId(){
  41.  
    return id;
  42.  
    }
  43.  
    public String getName(){
  44.  
    return name;
  45.  
    }
  46.  
    public int getAge(){
  47.  
    return age;
  48.  
    }
  49.  
    @Override
  50.  
    public String toString(){
  51.  
    return "id:" id " name: " name " age:" age;
  52.  
    }
  53.  
    public void setId(int id){
  54.  
    this.id=id;
  55.  
    }
  56.  
    public void setName(String name){
  57.  
    this.name=name;
  58.  
    }
  59.  
    public void setAge(int age){
  60.  
    this.age=age;
  61.  
    } }
  62.  
    }
学新通

4、测试

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

界面返回信息 

 学新通

 接收方控制台输出

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaaeba
系列文章
更多 icon
同类精品
更多 icon
继续加载