当前位置: 移动技术网 > IT编程>开发语言>Java > SpringCloud使用Feign文件上传、下载

SpringCloud使用Feign文件上传、下载

2019年07月19日  | 移动技术网IT编程  | 我要评论

文件上传、下载也是实际项目中会遇到的场景,本篇我们介绍下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 

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

服务提供方java代码:

/**
  * 文件(二进制数据)下载
  * @param filetype 文件类型
  * @return
  */
  @requestmapping("/downloadfile")
  public responseentity<byte[]> downloadfile(string filetype,httpservletrequest request ){
   
   system.out.println(request.getparameter("filetype"));
   system.out.println("参数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;
 }

服务提供方feign api接口 

@requestmapping("/downloadfile")
  public responseentity<byte[]> downloadfile(@requestparam(value = "filetype") string filetype
    );

服务消费方

@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 ;
}

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

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网