SpringCloud使用Feign文件上传、下载

文件上传、下载也是实际项目中会遇到的场景,本篇我们介绍下springcloud中如何使用feign进行文件上传与下载。

还是使用feign 进行http的调用。

一、Feign文件上传

服务提供方java代码:

/**
 * 文件上传
 * @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";
}

服务提供方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);

服务消费方:

pom.xml 


 
  io.github.openfeign.form
  feign-form
  3.0.3
  
 
  io.github.openfeign.form
  feign-form-spring
  3.0.3

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 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文件下载

服务提供方java代码:

/**
  * 文件(二进制数据)下载
  * @param fileType 文件类型
  * @return
  */
  @RequestMapping("/downloadFile")
  public ResponseEntity downloadFile(String fileType,HttpServletRequest request ){
   
   System.out.println(request.getParameter("fileType"));
   System.out.println("参数fileType: "+fileType);
   
   HttpHeaders headers = new HttpHeaders();
   ResponseEntity 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(bytes, headers, HttpStatus.OK);
   
  } catch (Exception e) {
   e.printStackTrace();
  }finally {
   if(in!=null) {
    try {
     in.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
   
   return entity;
 }

服务提供方feign api接口 

@RequestMapping("/downloadFile")
  public ResponseEntity downloadFile(@RequestParam(value = "fileType") String fileType
    );

服务消费方

@ResponseBody
  @RequestMapping("/user_downloadFile")
  public Object user_downloadFile(HttpServletRequest request,HttpServletResponse response,
    String fileType) {
   ResponseEntity entity = userProControllerApi.downloadFile(fileType);
   System.out.println( entity.getStatusCode());
   return entity ;
}

注:实际项目中如果上传的文件太大,可以使用ftp服务器保存上传的文件,直接在controller端调用ftp接口即可。

如果下载的文件太大,则调用service端接口可返回一个ftp文件资源路径,然后在controller端调用ftp下载文件即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(SpringCloud使用Feign文件上传、下载)