欢迎光临
感受代码之美

Spring Cloud Feign如何获得接口返回的文件流

本文通过Feign实现微服务间接口调用,返回stream,从而让通过stream实现文件的下载、页面图片、视频等展示成为可能,实现代码如下。

服务端

Controller

@GetMapping(value = "/fileStream/{fileId}")
public void getFileStream(@PathVariable(value = "fileId") String fileId, HttpServletResponse httpServletResponse) {
    service.getFileStream(fileId, httpServletResponse);
}

Service

public void getFileStream(String fileId, HttpServletResponse response) {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        UploadFileInfo uploadFileInfo = selectByPrimaryKey(fileId);
        String filePath = uploadFileInfo.getFilePath();
        File file = new File(filePath);
        response.setCharacterEncoding("utf-8");
        String contentType = contentType(filePath);
        response.setContentType(contentType);
        bis = new BufferedInputStream(new FileInputStream(file));
        bos = new BufferedOutputStream(response.getOutputStream());
        readStream(bis, bos);
        bos.flush();
    } catch (Exception e) {
        e.printStackTrace();
        response.setStatus(500);
    } finally {
        closeBuffered(bis, bos);
    }
}

微服务客户端

定义接口

import feign.Response;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "COMMON-UPLOAD")
@Service
public interface UploadService {
    @GetMapping(value = "/uploads/fileStream/{fileId}", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Response getFileStream(@PathVariable("fileId") String fileId);
}

客户端Controller

@GetMapping("/identityInfoStream/{organId}")
public void getIdentityInfoStream(@PathVariable String organId, HttpServletResponse httpServletResponse) {
    organStaffService.getIdentityInfoStream(organId, httpServletResponse);
}

客户端Service

public void getIdentityInfoStream(String organId, HttpServletResponse httpServletResponse) {
    Example example = new Example(PubOrganStaff.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("organId", organId);
    criteria.andEqualTo("extType", "identityInfo");
    List<PubOrganStaff> staffList = selectByExample(example);
    String fileId = ObjectUtils.isEmpty(staffList) ? null : staffList.get(0).getExtValue();

    // Feign接口
    Response response = uploadService.getFileStream(fileId);

    Response.Body body = response.body();

    InputStream fileInputStream;
    OutputStream outStream;
    try {
        fileInputStream = body.asInputStream();
        outStream = httpServletResponse.getOutputStream();

        byte[] bytes = new byte[1024];
        int len;
        while ((len = fileInputStream.read(bytes)) != -1) {
            outStream.write(bytes, 0, len);
        }
        fileInputStream.close();
        outStream.close();
        outStream.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
赞(0)
未经允许禁止转载:四个空格 » Spring Cloud Feign如何获得接口返回的文件流