ダウンロードコード
@RequestMapping(= { "/データ/ドッキング/絵/ {ID} / {EMPI}"値}) パブリック JsonApi picToJSP(@PathVariable文字列ID、@PathVariable( "EMPI" )文字列EMPI、 @Validated({BaseEntity.SelectOne 。クラス})tFileWork0 tFileWork0、HttpServletRequestのリクエスト、 HttpServletResponseの応答){ でのFileInputStream。 文字列名 = nullを。 Yhxdはyhxd = 新しいYhxd(); yhxd.setEmpi(EMPI)。 一覧 <地図<文字列、オブジェクト>> yhxdList = yhxdService.getList(yhxd)。 もし(yhxdList!= nullを&& yhxdList.size()> 0 ){ 名前 = yhxdList.get(0)に.get( "YHMC")のtoString()+ " -心电图- "。; } tFileWork0.setId(ID)。 地図 <文字列、オブジェクト>マップ= tFileWork0Service.getOne(tFileWork0)。 場合(==マップヌル){ 返す 新しいJsonApi(ApiCodeEnum.NOT_FOUND)を、 } 他{ 文字列のURL = map.get( "FILE_PATH")のtoString()+ map.get( "FILE_NAME"。 )。 ストリングNA = map.get( "FILE_NAME" ).toString(); 文字列のファイル名 =名+ NA。 試す{ // 图片读取路径 文字列imgUrl = "C:/ユーザー/ chenyan /" + URL。 中 = 新しいFileInputStreamを(imgUrl)。 int型私は= )(in.available。 バイト []データ= 新しい バイト[i]は、 in.read(データ)。 )(in.close。 response.setContentType( "アプリケーション/オクテットのストリーム;のcharset = UTF-8" )。 response.setHeader("コンテンツの廃棄"、 "添付ファイル;ファイル名=" + java.net.URLEncoder.encode(ファイル名、 "UTF-8" )); OutputStreamのOutputStream = 新なBufferedOutputStream(response.getOutputStream()); outputStream.write(データ)。 outputStream.flush(); outputStream.close(); } キャッチ(例外e){ e.printStackTrace(); } 戻り 新しいJsonApi(ApiCodeEnum.OK)を、 } }
グローバル例外ハンドラ
パッケージcom.data.docking.exception。 輸入org.slf4j.Logger; 輸入org.slf4j.LoggerFactory。 輸入org.springframework.web.bind.annotation.ControllerAdvice。 輸入org.springframework.web.bind.annotation.ExceptionHandler; 輸入org.springframework.web.bind.annotation.ResponseBody。 輸入com.data.docking.tools.ApiCodeEnum; 輸入com.data.docking.tools.JsonApi; / ** *著作権©2019。 * * @author :ChenYan * @date:2019年10月10日 * @description:全局异常处理器 * / @ControllerAdvice @ResponseBody パブリック クラスGlobalExceptionHandler { ロガーログ = LoggerFactory.getLogger(GlobalExceptionHandler。クラス)。 @ExceptionHandler(例外クラス) パブリックJsonApi defaultErrorHandler(例外e){ e.printStackTrace(); log.error( "エラーメッセージ:{}" 、e.getMessage())。 返す 新しいJsonApi(ApiCodeEnum.ERROR).setMsg(e.getMessage()); } }
ファイルをダウンロードするときにエラー
ちょうど必要性
@ExceptionHandler(Exception.class その上@ExceptionHandler(BindException.class)へ)。