Feign upload and download file function

Get into the habit of writing together! This is the 15th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .

FeignUploadDownload

problem background

Based on some of the current mainstream frameworks, which are basically used 微服务, the file management function is often fileextracted to an independent 微服务example file微服务.

Front-end requests, all call the function file微服务of file summation in a unified way ! ! !上传下载

There seems to be nothing wrong with such a design, unified management! ! !

image.png

Because the file is uploaded, it may also need to be written 业务数据, for example, 附件的信息etc., which has to be extracted into an independent microservice for 前端use.

Then there will be a problem: ! ! ! T_T

1. What if my attachments are not uploaded through the frontend? I am a backend service and need to save an attachment, so what are you doing?

2. A service in my backend needs to read a file and parse it. Because it is a distributed architecture, it may be file微服务deployed 服务器Athere . How should we 后端服务deploy it 服务B, so that 磁盘路径the file cannot be read at all, so how do you deal with it? Woolen cloth?

Hmmmm, what you said makes sense, this is indeed a big problem! ! !

image.png

Don't panic, we will solve this problem today! ! !

Because the invocation of the interface between our services, in the 微服务system, is generally done through feignde-tuning. So, can the file also be feigncalled through?

The answer is definitely yes, haha! ! ! ^_^

Not much nonsense, let’s get to the code! ! !

Feign interface preparation

  • Define an Feignupload and download interface
@FeignClient(name = "file", configuration = {FileClient.MultipartSupportConfig.class})
public interface FileClient {
    /**
     * 上传文件
     * @param file
     * @return
     */
    @RequestMapping(path = "/file/upload", consumes = {"multipart/form-data"})
    JsonResult upload(@RequestPart(name="file") MultipartFile file);

    /**
     * 下载文件
     */
    @GetMapping(path = "/file/download/{fileId}")
    Response download(@PathVariable(name = "fileId") String fileId);
       
    public class MultipartSupportConfig {
        @Bean
        @Primary
        @Scope("prototype")
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
}
复制代码
  • file微服务Provide file 上传interface
@RequestMapping("/file/upload")
public JsonResult upload(MultipartHttpServletRequest request) throws Exception {
    JsonResult jsonResult = new JsonResult();
    String timestamp = request.getParameter("time");
    MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
    List<MultipartFile> files = multiFileMap.get("files");
    if (BeanUtil.isEmpty(files)) {
        files = multiFileMap.get("file");
    }
    Iterator<MultipartFile> it = files.iterator();
    while (it.hasNext()) {
        MultipartFile multipartFile = it.next();
        //保存到磁盘
        multipartFile.transferTo(new File("文件路径"));
        
        //记录文件上传业务数据
        ...
        xxxxFileDao.saveFile(xxxFile);
    }
    jsonResult.setSuccess(true);
    jsonResult.setData(fileList);
    jsonResult.setMessage("成功上传!");
    return jsonResult;
}
复制代码
  • file微服务Provide file 下载interface
@GetMapping("/download/{fileId}")
public void feigndownloadOne(@PathVariable("fileId") String fileId, HttpServletResponse response) throws Exception {
    
    //通过fileId查找文件
    ...
    xxxFile = xxxxFileDao.getFile(fileId);
    File file = new File("文件路径");
    
    String filename = xxxFile.getName();
    String type ="attachment";
    response.setHeader("Content-Disposition", type + ";filename=" + filename);
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream buff = null;
    OutputStream out = null;
    try {
        buff = new BufferedInputStream(fis);
        out = response.getOutputStream();
        IOUtils.copy(buff, out);
    } finally {
        out.flush();//注意out流,对象不能close
        fis.close();
        buff.close();
    }
}
复制代码

下载这个接口,有个比较重要的地方就是,out流,不能close(),否则就算获取到该流,也无法处理业务逻辑。

image.png

好了,上面就是咋们定义好的feign接口,和提供服务的file微服务上传下载接口!!!

上传下载接口,只是给出一个demo,大家伙,可自行修改,相应的业务逻辑!!!^_^

Feign上传下载

接下来,咋们就开始使用到这个feign上传下载功能!!!

image.png

  • 上传feign接口,使用

import java.io.File;
import java.io.FileInputStream;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;
import org.apache.http.entity.ContentType;
 
 
File pdfFile = new File("D://test.pdf");
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
					ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);

//上传
JsonResult jsonResult = fileClient.upload(multipartFile);
if (jsonResult.isSuccess() && jsonResult.getCode() == 200) {
   
}
复制代码

注意了 注意了 注意了!!!

这里,有个要注意的地方,因为我们的upload接口,即controller接口一般使用MultipartFile对象接收文件,那么我们就得把File,对象先转成MultipartFile对象,才可以调通这个接口!!!

转换方式,看上面代码即可!!!

好了,这个就是上传文件的示例,也是比较简单啦!!!

  • 下载feign接口,使用
import feign.Response;

//文件流
Response response = fileClient.download(fileId);
InputStream in = null;
if(response.status() == 200){
    Response.Body body = response.body();
    in = body.asInputStream();
    
    //拿到这个in流,处理相应的业务逻辑
    ...

    
    //处理完成后,记得关闭一下inputStream流
    if(in != null){
        in.close();
    }
}
复制代码

注意了 注意了 注意了!!!

这里有两个地方需要注意:

1.因为我们的download接口,out流是还没有close(),所以这里的in流,用完得close()一下。

2.这里的代码,在开发过程中,不能打断点调试,不能打断点调试,不能打断点调试!!!

重要的事情得说三遍,不能打断点调试,因为你断点调试后,这里的in流,会因为执行了toString()方法,导致in流被关闭了,无法再使用,即不能处理业务逻辑代码了!!!

image.png

好了,这个就是上传文件的示例,也是比较简单啦!!!

好了,Feign上传和下载,大概的实现,就是这样了!!!^_^

Then we can happily write code! ! ! ^_^

image.png

Guess you like

Origin juejin.im/post/7086827087260024845