首页 > 编程语言 > 详细

java后端发送formdata上传文件

时间:2020-01-10 11:28:38      阅读:1431      评论:0      收藏:0      [点我收藏+]

今天想实现 java 后端发送 formdata 上传文件,为了以后查找方便,特此记录下来

上一次使用 WebClient 实现远程调用 (一个非阻塞、响应式的HTTP客户端,它以响应式被压流的方式执行HTTP请求) 查看

现在使用的 RestTemplate

RestTemplate 是在客户端访问 Restful 服务的一个核心类

默认使用 JDK 提供的包去建立HTTP连接

为每种 HTTP 请求都实现了相关的请求封装方法

  public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)

        url -> URI类型的请求路径

        request -> 请求体对象

        responseType -> 响应数据类型

package com.example.hystrix.controller;

import org.springframework.core.io.FileSystemResource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;

@RestController
public class DemoController {

    @RequestMapping("/upload")
    public String upload() {

        String url = "http://localhost:2001/api/upload"; //上传的地址
        String filePath = "E:\\test\\test.dxf";

        RestTemplate rest = new RestTemplate();
        FileSystemResource resource = new FileSystemResource(new File(filePath));
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("files", resource); //MultipartFile的名称
        String rs = rest.postForObject(url, param, String.class);
        System.out.println(rs);
        return rs;
    }
}

或者

  public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType)

        url -> URI类型的请求路径

        method-> 请求方式

        requestEntity-> 请求体

        responseType -> 响应数据类型

package com.example.hystrix.controller;

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;

@RestController
public class DemoController {

    @RequestMapping("/upload")
    public String upload() {

        String url = "http://localhost:2001/api/upload"; //上传的地址
        String filePath = "E:\\test\\test.dxf";

        RestTemplate rest = new RestTemplate();
        FileSystemResource resource = new FileSystemResource(new File(filePath));
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("files", resource); //MultipartFile的名称
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param);
        ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);
        String rs = responseEntity.getBody();
        System.out.println(rs);
        return rs;
    }
}

 

java后端发送formdata上传文件

原文:https://www.cnblogs.com/baby123/p/12174942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!