java接受formdata文件上传_java后端发送formdata上传文件

这篇博客记录了如何在Java后端使用RestTemplate发送FormData请求上传文件,包括两种不同的实现方式,一种通过`postForObject`,另一种通过`exchange`方法。示例代码详细展示了如何创建请求体和调用API进行文件上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

现在使用的 RestTemplate

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

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

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

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

url -> URI类型的请求路径

request -> 请求体对象

responseType -> 响应数据类型

packagecom.example.hystrix.controller;importorg.springframework.core.io.FileSystemResource;importorg.springframework.util.LinkedMultiValueMap;importorg.springframework.util.MultiValueMap;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.client.RestTemplate;importjava.io.File;

@RestControllerpublic classDemoController {

@RequestMapping("/upload")publicString upload() {

String url= "http://localhost:2001/api/upload"; //上传的地址

String filePath = "E:\\test\\test.dxf";

RestTemplate rest= newRestTemplate();

FileSystemResource resource= new FileSystemResource(newFile(filePath));

MultiValueMap param = new LinkedMultiValueMap<>();

param.add("files", resource); //MultipartFile的名称String rs= rest.postForObject(url, param, String.class);

System.out.println(rs);

returnrs;

}

}

或者

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

url -> URI类型的请求路径

method-> 请求方式

requestEntity-> 请求体

responseType -> 响应数据类型

packagecom.example.hystrix.controller;importorg.springframework.core.io.FileSystemResource;importorg.springframework.http.HttpEntity;importorg.springframework.http.HttpMethod;importorg.springframework.http.ResponseEntity;importorg.springframework.util.LinkedMultiValueMap;importorg.springframework.util.MultiValueMap;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.client.RestTemplate;importjava.io.File;

@RestControllerpublic classDemoController {

@RequestMapping("/upload")publicString upload() {

String url= "http://localhost:2001/api/upload"; //上传的地址

String filePath = "E:\\test\\test.dxf";

RestTemplate rest= newRestTemplate();

FileSystemResource resource= new FileSystemResource(newFile(filePath));

MultiValueMap param = new LinkedMultiValueMap<>();

param.add("files", resource); //MultipartFile的名称HttpEntity> httpEntity = new HttpEntity>(param);

ResponseEntity responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class);

String rs=responseEntity.getBody();

System.out.println(rs);

returnrs;

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值