Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the upload and download of files in WebClient

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report