| | |
| | | import java.io.IOException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.Set; |
| | | import java.util.UUID; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | |
| | | @RestController |
| | | @RequestMapping("/file/") |
| | | public class FileController { |
| | | |
| | | private static final String MANAGE_MEDIA_EXTENSIONS = "mp4,avi,rmvb,mov,wmv,flv,mkv,mpeg,mpg"; |
| | | |
| | | @Autowired |
| | | private FileUploadConfig fileUploadConfig; |
| | |
| | | // 获取文件后缀 |
| | | String ext = filename.substring(filename.lastIndexOf(".") + 1); |
| | | // 检查文件类型 |
| | | String allowedExt = getAllowedExt(fileUploadConfig.getDocExt()); |
| | | String allowedExt = getManageAllowedExt(); |
| | | if (!isAllowedExt(ext, allowedExt)) { |
| | | return invalidExtResult(allowedExt); |
| | | } |
| | |
| | | return fileUploadConfig.getAllowExt(); |
| | | } |
| | | |
| | | private boolean isAllowedExt(String ext, String allowedExt) { |
| | | String getManageAllowedExt() { |
| | | return mergeAllowedExt(getAllowedExt(fileUploadConfig.getDocExt()), MANAGE_MEDIA_EXTENSIONS); |
| | | } |
| | | |
| | | static String mergeAllowedExt(String... extensionGroups) { |
| | | Set<String> extensions = new LinkedHashSet<>(); |
| | | if (extensionGroups != null) { |
| | | for (String group : extensionGroups) { |
| | | if (group == null) { |
| | | continue; |
| | | } |
| | | for (String item : group.split(",")) { |
| | | String normalizedItem = item.trim(); |
| | | if (normalizedItem.startsWith(".")) { |
| | | normalizedItem = normalizedItem.substring(1); |
| | | } |
| | | if (!normalizedItem.isEmpty()) { |
| | | extensions.add(normalizedItem.toLowerCase()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return String.join(",", extensions); |
| | | } |
| | | |
| | | static boolean isAllowedExt(String ext, String allowedExt) { |
| | | if (ext == null || allowedExt == null || allowedExt.trim().length() == 0) { |
| | | return false; |
| | | } |
| | |
| | | private AjaxResult invalidExtResult(String allowedExt) { |
| | | return AjaxResult.error("上传文件格式不正确,仅支持" + (allowedExt == null ? "" : allowedExt)); |
| | | } |
| | | } |
| | | } |