2021-03-31

feign.codec.EncodeException: Could not write JSON: getOutputStream() has already been called for this response; nested exception is com.fasterxml.jackson.databind.JsonMappingException: getOutputStream() has already been called for this response (through reference chain: com.beantechs.tsp.boot.service.logging.ResponseWrapper[“response”]->ch.qos.logback.access.servlet.TeeHttpServletResponse[“response”]->org.apache.catalina.connector.ResponseFacade[“writer”])\r\n\tat

后面的异常就不贴了
我的场景是用feign去下载模板出现了这个异常。解决方案如下:

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private CmpTestClient cmpTestClient;

    @GetMapping("/getFile")
    public void test(HttpServletResponse response) throws IOException {
        ServletOutputStream os = null;
        //调feign获取feignResponse
        Response feignResponse = cmpTestClient.getFile();
        try {
            InputStream inputStream = feignResponse.body().asInputStream();
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("import.excel", "UTF-8"));
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            os = response.getOutputStream();
            byte[] b = toByteArray(inputStream);
            os.write(b);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/text;charset=UTF-8");
        }finally {
            try {
                os.close();
            } catch (Exception e) {
            }
        }
    }
   /**
     * InputStream 转换成byte[]
     * @param input
     * @return
     * @throws IOException
     */
    private static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024*4];
        int n;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }
}

@FeignClient(name = "cmp", url = "10.33.241.208:8080")
public interface CmpTestClient {

    @GetMapping("/cmp/file/download")
    feign.Response  getFile();
}

其实就是一个demo

那么下面也写一个用feign上传的demo

   @ApiOperation("批量导入sim卡模板文件")
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Response upload(@RequestPart("file") MultipartFile file, @RequestParam("apn1Status") String apn1Status,
                           @RequestParam("apn2Status") String apn2Status,
                           @RequestHeader String operator) {
        return gwmCmpClient .fileUpload(file, apn1Status, apn2Status, channel, countryCode, factory, mnoCode, mnoName, smsStatus, voiceStatus, operator);
public interface GwmCmpClient {
   /**
     * 批量导入sim卡模板文件
     */
    @PostMapping(value = CMP_PATH + "/file/upload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Response fileUpload(@RequestPart("file") MultipartFile file, @RequestParam("apn1Status") String apn1Status,
            @RequestParam("apn2Status") String apn2Status,@RequestHeader String operator);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值