码迷,mamicode.com
首页 > 其他好文 > 详细

feign 调用注意事项

时间:2021-06-02 20:54:56      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:https   tco   图片服务器   _for   catch   close   文件   获取文件   long   

参考链接:

https://blog.csdn.net/wahaha13168/article/details/81211002

https://www.cnblogs.com/merryyou/p/11670171.html

 feign 接口返回流

服务提供者

@GetMapping("/{id}")
    public void queryJobInfoLogDetail(@PathVariable("id") Long id, HttpServletResponse response) {

        File file = new File("xxxxx");
        InputStream fileInputStream = new FileInputStream(file);
        OutputStream outStream;
        try {
            outStream = response.getOutputStream();

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outStream.write(bytes, 0, len);
            }
            fileInputStream.close();
            outStream.close();
            outStream.flush();
        } catch (IOException e) {
            log.error("exception", e);
        }
    }

client 客户端

@GetMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    feign.Response queryJobInfoLogDetail(@PathVariable("id") Long id);

服务消费者

@GetMapping("/{id}")
    public void queryJobInfoLogInfoList(@PathVariable("id") Long id, HttpServletResponse servletResponse) {

        Response response = apiServices.queryJobInfoLogDetail(id);
        Response.Body body = response.body();

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

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outStream.write(bytes, 0, len);
            }
            fileInputStream.close();
            outStream.close();
            outStream.flush();
        } catch (Exception e) {

        }
    }

2.Could not resolve view with name xxx 

问题:spring会认为你请求的结果是一个html的视图,所以才抛出了上面的异常。

解决方法:在请求的方法上加上@ResponseBody,或者有需要在控制层上加 @RestController 注解

3. 上传文件

服务调用者

@PostMapping("/xxx/file")
public xx uploadOrderFilesToOSS(@ApiParam("附件") @RequestParam("file") MultipartFile[] file) {
   return xxxService.uploadOrderFilesToOSS(file);
}

Feign

@PostMapping(value = "/file", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
xxx uploadSigleFile(@RequestParam("path") String path, @RequestPart("file") MultipartFile file);

服务提供者

@PostMapping(value = "/file", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public xxx uploadSigleFile(@RequestParam("path") String path, @RequestPart("file") MultipartFile file) {
  return fileService.uploadFileToOSS(path, file);
}

4. feign  PathVariable获取文件后缀

今天在做一个返回图片接口,因鉴于通过get请求传参获取文件名和文件路径会出现严重漏洞问题,现采用直接以文件路径的方式访问文件下载,url这样的写法@RequestMapping(value = "/content/{fileName}", method = RequestMethod.GET)会导致文件类型丢失。

解决方案:

在@RequestMapping的value中使用SpEL来表示,value中的{fileName}换成{fileName:.+}。

@RequestMapping(value = "/content/{fileName:.+}", method = RequestMethod.GET)

比如我从图片服务器获取某一文件,路径是localhost:8080/file/test.jpg,

通过@PathVariable应该获取test.jpg,如果不做任何处理,结果获取到的是test。

解决方法:

@RequestMapping("/file/{filename:.+}")

feign 调用注意事项

标签:https   tco   图片服务器   _for   catch   close   文件   获取文件   long   

原文地址:https://www.cnblogs.com/zch-admin/p/14840171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!