diff --git a/src/main/java/im/zhaojun/common/exception/GlobleExceptionHandler.java b/src/main/java/im/zhaojun/common/exception/GlobleExceptionHandler.java index 32a7556..da630e4 100644 --- a/src/main/java/im/zhaojun/common/exception/GlobleExceptionHandler.java +++ b/src/main/java/im/zhaojun/common/exception/GlobleExceptionHandler.java @@ -16,12 +16,12 @@ import org.springframework.web.bind.annotation.ResponseStatus; * @author zhaojun */ @ControllerAdvice -@ResponseBody public class GlobleExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobleExceptionHandler.class); @ExceptionHandler(SearchDisableException.class) + @ResponseBody @ResponseStatus(code= HttpStatus.INTERNAL_SERVER_ERROR) public ResultBean searchDisableExceptionHandler(SearchDisableException e) { if (log.isDebugEnabled()) { @@ -32,6 +32,7 @@ public class GlobleExceptionHandler { @ExceptionHandler + @ResponseBody @ResponseStatus public ResultBean searchDisableExceptionHandler(StorageStrategyUninitializedException e) { if (log.isDebugEnabled()) { @@ -40,6 +41,16 @@ public class GlobleExceptionHandler { return ResultBean.error(e.getMessage()); } + + /** + * 不存在的文件异常 + */ + @ExceptionHandler({NotExistFileException.class}) + @ResponseStatus(HttpStatus.NOT_FOUND) + public String notExistFile(Exception ex) { + return "error/404"; + } + /** * 捕获 ClientAbortException 异常, 不做任何处理, 防止出现大量堆栈日志输出, 此异常不影响功能. */ @@ -53,6 +64,7 @@ public class GlobleExceptionHandler { } @ExceptionHandler + @ResponseBody @ResponseStatus(code= HttpStatus.INTERNAL_SERVER_ERROR) public ResultBean searchDisableExceptionHandler(Exception e) { if (log.isDebugEnabled()) { diff --git a/src/main/java/im/zhaojun/common/exception/NotExistFileException.java b/src/main/java/im/zhaojun/common/exception/NotExistFileException.java new file mode 100644 index 0000000..411de10 --- /dev/null +++ b/src/main/java/im/zhaojun/common/exception/NotExistFileException.java @@ -0,0 +1,29 @@ +package im.zhaojun.common.exception; + +/** + * @author zhaojun + * @date 2020/1/20 22:15 + */ +public class NotExistFileException extends RuntimeException { + + public NotExistFileException() { + super(); + } + + public NotExistFileException(String message) { + super(message); + } + + public NotExistFileException(String message, Throwable cause) { + super(message, cause); + } + + public NotExistFileException(Throwable cause) { + super(cause); + } + + protected NotExistFileException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/src/main/java/im/zhaojun/common/util/StringUtils.java b/src/main/java/im/zhaojun/common/util/StringUtils.java index dcce784..d498112 100644 --- a/src/main/java/im/zhaojun/common/util/StringUtils.java +++ b/src/main/java/im/zhaojun/common/util/StringUtils.java @@ -50,7 +50,7 @@ public class StringUtils { path = DELIMITER + path; } - if (domain.charAt(domain.length() - 1) == DELIMITER) { + if (domain != null && domain.charAt(domain.length() - 1) == DELIMITER) { domain = domain.substring(0, domain.length() - 2); } diff --git a/src/main/java/im/zhaojun/local/controller/LocalController.java b/src/main/java/im/zhaojun/local/controller/LocalController.java index ffda9b0..50f0099 100644 --- a/src/main/java/im/zhaojun/local/controller/LocalController.java +++ b/src/main/java/im/zhaojun/local/controller/LocalController.java @@ -1,6 +1,7 @@ package im.zhaojun.local.controller; import cn.hutool.core.util.URLUtil; +import im.zhaojun.common.exception.NotExistFileException; import im.zhaojun.common.util.StringUtils; import im.zhaojun.local.service.LocalServiceImpl; import org.springframework.core.io.FileSystemResource; @@ -42,7 +43,7 @@ public class LocalController { private ResponseEntity export(File file) { if (!file.exists()) { - return ResponseEntity.notFound().build(); + throw new NotExistFileException(); } diff --git a/src/main/resources/templates/error/404.html b/src/main/resources/templates/error/404.html new file mode 100644 index 0000000..e6d6ad5 --- /dev/null +++ b/src/main/resources/templates/error/404.html @@ -0,0 +1,187 @@ + + + + + + 404 ERROR + + + +
+
+
+

+ 404很抱歉,您要访问的文件/页面不存在! +

+
+
+

可能原因:

+
    +
  1. 网络信号差不稳定
  2. +
  3. 找不到请求的页面
  4. +
  5. 输入的网址不正确
  6. +
+
+
+

可以尝试:

+
    +
  1. 返回首页
  2. +
  3. 留言反馈
  4. +
  5. 联系站长
  6. +
+
+
+
+
+
+ + + + \ No newline at end of file