Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the upload and download of files in WebClient

Shulou Source: shulou.com Published: 2022-06-01 00:41:24 10月18日 Update

In this issue, Xiaobian will bring you about how to upload and download files in WebClient. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.

WebClient is an HTTP client library provided since Spring 5. In order to use WebClient for file upload and download, we need to write a server-side program that supports file upload and download. Please refer to an article I wrote earlier: SpringBoot implements local storage file upload and provides HTTP access services. After completing this study, you can obtain

A file upload service endpoint with access service URI "/upload"

The service endpoint successfully uploads the file and returns an HTTP connection that can be used to download the file.

Below we begin to learn to use WebClient is HTTP client library, file upload and download.

I. File upload

A file upload and receive server is set up on local port 8888, and the service endpoint is "/upload". Upload a file "D:\data\local\splash.png" from your local disk

@SpringBootTestclass UpDownLoadTests { //create webClient private WebClient webClient = WebClient.builder() .baseUrl("http://localhost:8888/") .build(); [@Test](https://my.oschina.net/azibug) void testUpload() { //File to be uploaded (exists on client local disk) String filePath = "D:\\data\\local\\splash.png"; //Encapsulate request parameters FileSystemResource resource = new FileSystemResource(new File(filePath)); MultiValueMap param = new LinkedMultiValueMap(); param.add("uploadFile", resource); //Server MultipartFile uploadFile //param.add("param1", "test"); //If the server accepts additional parameters, it can pass //Send request Mono mono = webClient .post() // POST request .uri("/upload") / .contentType(MediaType.APPLICATION_FORM_URLENCODED) .body(BodyInserters.fromMultipartData(param)) .retrieve() //retrieve the response body .bodyToMono(String.class); //Respond to data type conversion //Output results System.out.println(mono.block()); }}

The output printouts are as follows:

After the article is uploaded, it returns an HTTP URL access address, which can be accessed through a browser or downloaded through a program.

II. File download

After executing the following code, the downloaded file url will be saved correctly to the local disk directory

[@Test](https://my.oschina.net/azibug)void testDownLoad() throws IOException { Mono mono = webClient .get() // GET request .uri("/2020/08/22/0f0a353a-7033-4a13-832e-d043f6360514.png") //request path .accept(MediaType.APPLICATION_OCTET_STREAM) .exchange(); //Get Responder ClientResponse response = mono.block(); Resource resource = response.bodyToMono(Resource.class).block(); assert resource != null; Files.copy(resource.getInputStream(), Paths.get("D:\\data\\local\\splash-down.png" );} The above is how the file upload and download in WebClient shared by Xiaobian is. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Tags: Files services clients clients endpoints content parameters can be passed articles disks programs articles results paths analysis learning output success professional small and medium-sized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno macOS Microsoft Redmi