SpringCloud使用Feign文件上傳、下載
文件上傳、下載也是實(shí)際項目中會遇到的場景,本篇我們介紹下springcloud中如何使用feign進(jìn)行文件上傳與下載。
還是使用feign 進(jìn)行http的調(diào)用。
一、Feign文件上傳
服務(wù)提供方j(luò)ava代碼:
/**
* 文件上傳
* @param file 文件
* @param fileType
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "/uploadFile",
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String uploadFile(@RequestPart(value = "file") MultipartFile file,
@RequestParam(value = "fileType") String fileType,
HttpServletRequest request,HttpServletResponse response) {
System.out.println("fileType:"+fileType);
long size= file.getSize();
String contentType= file.getContentType();
String name = file.getName();
String orgFilename= file.getOriginalFilename();
System.out.println("size:"+size);
System.out.println("contentType:"+contentType);
System.out.println("name:"+name);
System.out.println("orgFilename:"+orgFilename);
String suffix = orgFilename.substring(orgFilename.lastIndexOf("."));//后綴
String uuid =UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
File dest = new File("f:/b13/"+uuid+suffix);
try {
file.transferTo(dest);
return dest.getCanonicalPath();//文件的絕對路徑
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
return "failure";
}
服務(wù)提供方Feign api接口:
@RequestMapping(method = RequestMethod.POST, value = "/uploadFile",
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String uploadFile(@RequestPart(value = "file") MultipartFile file, @RequestParam(value = "fileType") String fileType);
服務(wù)消費(fèi)方:
pom.xml
<!-- 引入文件feign文件上傳依賴 --> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.0.3</version> </dependency>
java代碼:
@Autowired
private UserProControllerApi userProControllerApi;
@ResponseBody
@RequestMapping("/user_uploadFile")
public Object user_uploadFile(HttpServletRequest request,HttpServletResponse response,
@RequestPart(value = "file") MultipartFile file, String fileType) {
System.out.println(fileType);
return userProControllerApi.uploadFile(file, fileType);
}
MultipartSupportConfig.java
@Configuration
public class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
@Primary
@Scope("prototype")
public Encoder feignEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
二、Feign文件下載
服務(wù)提供方j(luò)ava代碼:
/**
* 文件(二進(jìn)制數(shù)據(jù))下載
* @param fileType 文件類型
* @return
*/
@RequestMapping("/downloadFile")
public ResponseEntity<byte[]> downloadFile(String fileType,HttpServletRequest request ){
System.out.println(request.getParameter("fileType"));
System.out.println("參數(shù)fileType: "+fileType);
HttpHeaders headers = new HttpHeaders();
ResponseEntity<byte[]> entity = null;
InputStream in=null;
try {
in=new FileInputStream(new File("d:/myImg/001.png"));
byte[] bytes = new byte[in.available()];
String imageName="001.png";
//處理IE下載文件的中文名稱亂碼的問題
String header = request.getHeader("User-Agent").toUpperCase();
if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
imageName = URLEncoder.encode(imageName, "utf-8");
imageName = imageName.replace("+", "%20"); //IE下載文件名空格變+號問題
} else {
imageName = new String(imageName.getBytes(), "iso-8859-1");
}
in.read(bytes);
headers.add("Content-Disposition", "attachment;filename="+imageName);
entity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(in!=null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return entity;
}
服務(wù)提供方feign api接口
@RequestMapping("/downloadFile")
public ResponseEntity<byte[]> downloadFile(@RequestParam(value = "fileType") String fileType
);
服務(wù)消費(fèi)方
@ResponseBody
@RequestMapping("/user_downloadFile")
public Object user_downloadFile(HttpServletRequest request,HttpServletResponse response,
String fileType) {
ResponseEntity<byte[]> entity = userProControllerApi.downloadFile(fileType);
System.out.println( entity.getStatusCode());
return entity ;
}
注:實(shí)際項目中如果上傳的文件太大,可以使用ftp服務(wù)器保存上傳的文件,直接在controller端調(diào)用ftp接口即可。
如果下載的文件太大,則調(diào)用service端接口可返回一個ftp文件資源路徑,然后在controller端調(diào)用ftp下載文件即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot中interceptor攔截器未生效的解決
這篇文章主要介紹了spring boot中interceptor攔截器未生效的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
idea打包java可執(zhí)行jar包的實(shí)現(xiàn)步驟
這篇文章主要介紹了idea打包java可執(zhí)行jar包的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Java學(xué)習(xí)之如何進(jìn)行JSON解析
JSON(JavaScript?Object?Notation)是一種輕量級的數(shù)據(jù)交換格式,它算是JavaScript語言的一部分,與XML一樣都可以用于數(shù)據(jù)的存儲和傳輸,本文講給大家介紹如何進(jìn)行JSON解析,需要的朋友可以參考下2023-12-12
Java局部內(nèi)部類和匿名內(nèi)部類定義與用法實(shí)例分析
這篇文章主要介紹了Java局部內(nèi)部類和匿名內(nèi)部類,結(jié)合實(shí)例形式分析了java局部內(nèi)部類和匿名內(nèi)部類相關(guān)定義、原理與用法,需要的朋友可以參考下2019-08-08
Spring MVC訪問靜態(tài)文件_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Spring MVC訪問靜態(tài)文件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
SpringBoot簡單實(shí)現(xiàn)定時器過程
這篇文章主要介紹了SpringBoot簡單實(shí)現(xiàn)定時器過程,對于Java后端來說肯定實(shí)現(xiàn)定時功能肯定是使用到Spring封裝好的定時調(diào)度Scheduled2023-04-04

