文件下载代码

@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. window10体验terminal

    体验window-terminal(preview),很不错可以直接登录服务器通过ssh 01.打开store 02.搜索terminal 03.体验ssh主机 04.很爽,可以ctrl + 鼠标滚轮 ...

  2. Httpd服务入门知识-Httpd服务常见配置案例之DSO( Dynamic Shared Object)加载动态模块配置

    Httpd服务入门知识-Httpd服务常见配置案例之DSO( Dynamic Shared Object)加载动态模块配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.加载动 ...

  3. 执行shell脚本时提示/bin/sh^M: bad interpreter: No such file or directory

    执行脚本时提示解释器有问题,错误提示如下: 这种提示一般是脚本在windows系统之通过记事本写的,记事本修改过的文本,会默认在文本前面加上一些看不到的标记,导致shell脚本不能被shell解释器识 ...

  4. 个人作业第五次——Alpha项目测试

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/2019autumnsystemanalysisanddesign/ 这个作业的要求在哪里 https:/ ...

  5. Could not resolve com.android.support:design 28.0.0

    想试一试android的那个侧滑栏功能,需要用到 'com.android.support:design:28.0.0'建好之后一直报关于28.00的错误,一开始各种百度更改配置没吊用,最后感觉是网络 ...

  6. CodeForces - 348D:Turtles(LGV定理)

    题意:给定N*M的矩阵,'*'表示可以通过,'#'表示不能通过,现在要找两条路径从[1,1]到[N,M]去,使得除了起点终点,没有交点. 思路:没有思路,就是裸题.  Lindström–Gessel ...

  7. hive表分区相关操作

    Hive 表分区 Hive表的分区就是一个目录,分区字段不和表的字段重复 创建分区表: create table tb_partition(id string, name string) PARTIT ...

  8. 教你如何解决WIN 10系统睡眠无法唤醒

    1.右击“开始菜单”,选择“电源选项”.   2.选择“更改计算机睡眠时间”. 3.选择“更改高级电源设置”.   4.选择“还原计划默认值”.   5.在弹出的对话框中选择“是 ”.   6.最后选 ...

  9. CLR Exception---E0434352

    什么是CLR Exception---E0434352 CLR异常是.NET应用程序生成的异常类型.异常被封装在从System.exception类派生的类中.它的异常代码是0xE0434352,代码 ...

  10. Django 基础篇(二)视图与模板

    视图 在django中,视图对WEB请求进行回应 视图接收reqeust对象作为第一个参数,包含了请求的信息 视图就是一个Python函数,被定义在views.py中 #coding:utf- fro ...