文件下载代码

@RequestMapping(value = { "/data/docking/picture/{id}/{empi}" })
public JsonApi picToJSP(@PathVariable String id, @PathVariable("empi") String empi,
@Validated({ BaseEntity.SelectOne.class }) TFileWork0 tFileWork0, HttpServletRequest request,
HttpServletResponse response) { FileInputStream in;
String name = null; Yhxd yhxd = new Yhxd();
yhxd.setEmpi(empi);
List<Map<String, Object>> yhxdList = yhxdService.getList(yhxd);
if (yhxdList != null && yhxdList.size() > 0) {
name = yhxdList.get(0).get("YHMC").toString() + "-心电图-";
} tFileWork0.setId(id);
Map<String, Object> map = tFileWork0Service.getOne(tFileWork0);
if (map == null) {
return new JsonApi(ApiCodeEnum.NOT_FOUND);
} else {
String url = map.get("file_path").toString() + map.get("file_name");
String na = map.get("file_name").toString();
/*设置文件下載名称*/
String filename = name + na;
try {
// 图片读取路径
String imgUrl = "C:/Users/chenyan/" + url;
in = new FileInputStream(imgUrl);
int i = in.available();
byte[] data = new byte[i];
in.read(data);
in.close(); response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} return new JsonApi(ApiCodeEnum.OK);
} }

全局异常处理器

package com.data.docking.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import com.data.docking.tools.ApiCodeEnum;
import com.data.docking.tools.JsonApi; /**
* Copyright © 2019 .
*
* @author: ChenYan
* @date: 2019年10月10日
* @description: 全局异常处理器
*/
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(Exception.class)
public JsonApi defaultErrorHandler(Exception e) {
e.printStackTrace();
log.error("error msg:{}", e.getMessage());
return new JsonApi(ApiCodeEnum.ERROR).setMsg(e.getMessage());
}
}

在下载文件的时候报错

只需要把

 @ExceptionHandler(Exception.class) 改成 @ExceptionHandler(BindException.class)就可以了。

SpringBoot项目中的全局异常处理器 Failed to invoke @ExceptionHandler method的更多相关文章

  1. 添加@ControllerAdvice后报错 Failed to invoke @ExceptionHandler method

    首先.单独使用ControllerAdvice 无法正常工作.需要配合@EnableWebMvc 使用. @ControllerAdvice @EnableWebMvc pulbic class Ex ...

  2. SSM之全局异常处理器

    1. 异常处理思路 首先来看一下在springmvc中,异常处理的思路:   如上图所示,系统的dao.service.controller出现异常都通过throws Exception向上抛出,最后 ...

  3. Spring Boot 中全局异常处理器

    Spring Boot 中全局异常处理器,就是把错误异常统一处理的方法.等价于Springmvc中的异常处理器. 步骤一:基于前面的springBoot入门小demo修改 步骤二:修改HelloCon ...

  4. springmvc中拦截器与springmvc全局异常处理器的问题

    最近在做一个练手的小项目, 系统架构中用了springmvc的全局异常处理器, 做了系统的统一异常处理. 后来加入了springmvc的拦截器, 为了一些需求, 在拦截器中的 preHandle 方法 ...

  5. 【spring】-- springboot配置全局异常处理器

    一.为什么要使用全局异常处理器? 什么是全局异常处理器? 就是把错误异常统一处理的方法. 应用场景: 1.当你使用jsr303参数校验器,如果参数校验不通过会抛异常,而且无法使用try-catch语句 ...

  6. 【学习】SpringBoot之全局异常处理器

    /** * 全局异常处理器 **/ @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exceptio ...

  7. SpringBoot项目中遇到的BUG

    1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...

  8. springboot项目中接口入参的简单校验

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  9. 从源码看全局异常处理器@ExceptionHandler&@ExceptionHandler的生效原理

    1.开头在前 日常开发中,几乎我们的项目都会用到异常处理器,我们通常会定制属于自己的异常处理器,来处理项目中大大小小.各种各样的异常.配置异常处理器目前最常用的方式应该是使用@ControllerAd ...

随机推荐

  1. mysql设计规范和原则

    DB设计流程: 1,需求分析 2,ER设计 3,物理设计 需求分析的最佳实践是头脑风暴,把需求理解透彻.根据公司的现况和未来的发展,与pm一起来讨论. ER(EntiyRelation)设计阶段要确定 ...

  2. Httpd服务入门知识-Httpd服务常见配置案例之ServerSignature指令选项

    Httpd服务入门知识-Httpd服务常见配置案例之ServerSignature指令选项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ServerSignature指令概述 ...

  3. seaborn---画热力图

    1.引用形式: seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None ...

  4. 项目Beta冲刺(团队)——05.25(3/7)

    项目Beta冲刺(团队)--05.25(3/7) 格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺(团队) 团队名称:为了交项目干杯 作业目标:记录Beta敏捷冲刺第3 ...

  5. 利用Java反射机制优化简单工厂设计模式

    之前项目有个需求,审批流程的时候要根据配置发送信息:发送短信.发送邮件.当时看到这个就想到要用工厂模式,为什么要用工厂模式呢?用工厂模式进行大型项目的开发,可以很好的进行项目并行开发.就是一个程序员和 ...

  6. python-hashlib加密

    用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA512,MD5算法. 以下是算法示例: #!/usr/bin/env python # -*- c ...

  7. go中的事件对象time.Duration

    const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Sec ...

  8. 一起学Makefile(一)

    make和makefile makefile文件帮助我们记录了整个项目工程的所有需要编译的文件列表,这样我们在编译时仅需要输入简单的make命令就能编译出我们期望的结果. makefile文件反映了整 ...

  9. javaScript 迭代器

    for ...of 语句 "use strict"; var name = ['a','b','c']; var mark = [1, 2, 3]; for(var i of na ...

  10. JSX 简介

    JSX 简介 考虑如下变量声明: const element = <h1>Hello, world!</h1>; 这个有趣的标签语法既不是字符串也不是HTML. 它被称为JSX ...