public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
throws Exception {
if (file.getOriginalFilename() != null) {
int fileNamelength = file.getOriginalFilename().length();
if (fileNamelength > FILE_NAME_MAX) {
throw new Exception("檔案名過長");
}
}
long size = file.getSize();
if (size > DEFAULT_MAX_SIZE) {
throw new Exception("檔案過大");
}
String extension = getExtension(file);
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
throw new Exception("請上傳指定型別的檔案!");
}
}
uj5u.com熱心網友回復:
controller層,try {}catch(Exception e){}也可以列印捕獲的例外在日志里uj5u.com熱心網友回復:
你貼的是業務層代碼嗎。你是想在Controller層獲取你說的例外資訊吧,其實條件不滿足,不用throw 例外,你進方法的時候,new 一個map,像map.put("msg","檔案名過長"),然后return就行,在Controller就拿到了。如果想再高級一點,就自定義一個類,弄幾個屬性回傳也行
如果非要想通過throw Exception的方式,就自定義一個例外,比如MyFileException繼承Exception,再來個Msg屬性,在Controller層catch(MyFileException e)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/268146.html
標籤:Java EE
上一篇:導航自動換行什么原因
