In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article "how to use Spring Cloud Feign to achieve file upload and download" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "how to use Spring Cloud Feign to achieve file upload and download" article.
Feign framework does not do native support for file upload message body format, so it needs to be implemented by integration module feign-form.
Using Feign independently
Add module dependencies:
Io.github.openfeign feign-core 11.1 io.github.openfeign.form feign-form 3.8.0 commons-io commons-io 2.11.0 upload files
Define the interface:
Public interface FileUploadAPI {/ / upload file: parameter is single file object @ RequestLine ("POST / test/upload/single") @ Headers ("Content-Type: multipart/form-data") String upload (@ Param ("file") File file) / / upload file: parameter text multiple file objects @ RequestLine ("POST / test/upload/batch") @ Headers ("Content-Type: multipart/form-data") String upload (@ Param ("files") File [] files) / / upload file: parameter text multiple file objects @ RequestLine ("POST / test/upload/batch") @ Headers ("Content-Type: multipart/form-data") String upload (@ Param ("files") List files) / / upload file: the parameter is a file byte array (do not use this method to get the file name on the server) @ RequestLine ("POST / test/upload/single") @ Headers ("Content-Type: multipart/form-data") String upload (@ Param ("file") byte [] bytes) / / upload file: parameter is FormData object @ RequestLine ("POST / test/upload/single") @ Headers ("Content-Type: multipart/form-data") String upload (@ Param ("file") FormData photo); / / upload file: parameter is POJO object @ RequestLine ("POST / test/upload/single") @ Headers ("Content-Type: multipart/form-data") String upload (@ Param ("file") MyFile myFile) Class MyFile {@ FormProperty ("is_public") Boolean isPublic; File file; public Boolean getPublic () {return isPublic;} public void setPublic (Boolean aPublic) {isPublic = aPublic;} public File getFile () {return file } public void setFile (File file) {this.file = file;}
Call API:
FileAPI fileAPI = Feign.builder () .encoder (new FormEncoder ()) / must explicitly set the request parameter encoder .logger (new Slf4jLogger ()) .logLevel (Logger.Level.FULL) .target (FileAPI.class, "http://localhost:8080");File file1 = new File (" C:\\ Users\\ xxx\\ Downloads\\ test1.jpg ")) File file2 = new File ("C:\ Users\\ xxx\\ Downloads\\ test2.jpg"); / / upload file 1: parameter is file object fileAPI.upload (file1); / / upload file 2: parameter is byte array (note: file name cannot be obtained on server) byte [] bytes = FileUtils.readFileToByteArray (file1); fileAPI.upload (bytes); / / upload file 3: parameter is FormData object byte [] bytes = FileUtils.readFileToByteArray (file1) FormData formData = new FormData ("image/jpg", "test1.jpg", bytes); String result = fileAPI.upload (formData); / / upload file 4: parameter is POJO object FileAPI.MyFile myFile = new FileAPI.MyFile (); myFile.setPublic (true); myFile.setFile (file1); fileAPI.upload (myFile); / / upload file: parameter is multiple file fileAPI.upload (new File [] {file1, file2}); fileAPI.upload (Arrays.asList (new File [] {file1, file2})) Download a file
Define the interface:
Public interface FileDownloadAPI {/ / download file @ RequestLine ("GET / test/download/file") Response download (@ QueryMap Map queryMap);}
Call API:
/ / the Response object is returned when downloading the file, and there is no need to set the decoder FileAPI fileAPI = Feign.builder () .logger (new Slf4jLogger ()) .logLevel (Logger.Level.FULL) .target (FileAPI.class, "http://localhost:8080");String fileName =" test.jpg "; Map queryMap = new HashMap (); queryMap.put (" fileName ", fileName); Response response = fileAPI.download (queryMap) If (response.status () = 200) {File downloadFile = new File ("D:\\ Downloads\\", fileName); FileUtils.copyInputStreamToFile (response.body () .asInputStream (), downloadFile);} use Spring Cloud Feign
When using Feign to upload files in the Spring framework, you need to rely on feign-form and feign-form-spring. These two modules are already included in "Spring Cloud Feign". You only need to add spring-cloud-starter-openfeign dependencies.
Org.springframework.cloud spring-cloud-starter-openfeign 3.0.2 commons-io commons-io 2.11.0 upload files
Define interfaces and configurations:
@ FeignClient (value = "FileAPI", url = "http://localhost:8080", configuration = FileUploadAPI.FileUploadAPIConfiguration.class) public interface FileUploadAPI {/ * upload a single file * @ param file * @ return * / @ RequestMapping (value =" / test/upload/single ", method = RequestMethod.POST, headers =" Content-Type=multipart/form-data ") String upload (@ RequestPart (" file ") MultipartFile file) / * upload multiple files * @ param files * @ return * / @ RequestMapping (value = "/ test/upload/batch", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data") String upload (@ RequestPart ("files") List files); class FileUploadAPIConfiguration {@ Autowired private ObjectFactory messageConverters @ Bean public Encoder feignEncoder () {return new SpringFormEncoder (new SpringEncoder (messageConverters));} @ Bean public Logger feignLogger () {return new Slf4jLogger ();} @ Bean public Logger.Level feignLoggerLevel () {return Logger.Level.FULL;}}
Call API:
/ / upload single file File file = new File ("C:\ Users\\ xxx\\ Downloads\\ test1.jpg"); FileInputStream fis = new FileInputStream (file); MockMultipartFile mockMultipartFile = new MockMultipartFile ("file", file.getName (), "image/jpg", fis); this.fileUploadAPI.upload (mockMultipartFile); fis.close (); / / upload multiple files File file1 = new File ("C:\\ Users\\ xxx\\ Downloads\\ test1.jpg") File file2 = new File ("C:\ Users\ xxx\ Downloads\ test2.jpg"); FileInputStream fis1 = new FileInputStream (file1); FileInputStream fis2 = new FileInputStream (file2); MockMultipartFile F1 = new MockMultipartFile ("files", file1.getName (), "image/jpg", fis1); MockMultipartFile f2 = new MockMultipartFile ("files", file2.getName (), "image/jpg", fis2); this.fileUploadAPI.upload (Arrays.asList (new MockMultipartFile [] {F1, f2})); fis1.close (); fis2.close () Download a file
Define the interface:
@ FeignClient (value = "FileDownloadAPI", url = "http://localhost:8080", configuration = FileDownloadAPI.FileDownloadAPIConfiguration.class) public interface FileDownloadAPI {/ * download file * @ param fileName filename * @ return * / @ RequestMapping (value =" / test/download/file ", method = RequestMethod.GET) Response download (@ RequestParam (" fileName ") String fileName) / / the Response object is returned when downloading the file, and there is no need to set the decoder class FileDownloadAPIConfiguration {@ Bean public Logger feignLogger () {return new Slf4jLogger ();} @ Bean public Logger.Level feignLoggerLevel () {return Logger.Level.FULL;}
Call API:
String fileName = "test.jpg"; Response response = this.fileDownloadAPI.download (fileName); File destFile = new File ("D:\\ Downloads\\", fileName); / / use the org.apache.commons.io.FileUtils utility class to save the contents of the input stream to the file FileUtils.copyInputStreamToFile (response.body (). AsInputStream (), destFile). The above is about the content of this article on "how to use Spring Cloud Feign to upload and download files". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.