| | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | |
| | | /** |
| | | * 保存文件工具 |
| | |
| | | // 取yml中的路径 + / |
| | | private static String FILE_PATH; |
| | | |
| | | private static String WORD_URL_PATH; |
| | | |
| | | private static String[] ALLOWED; |
| | | |
| | | @Value("${file.path}") |
| | | private String file; |
| | | |
| | | @Value("${wordUrl}") |
| | | private String wordUrl; |
| | | |
| | | @Value("${file.allowed}") |
| | | private String[] allowed; |
| | |
| | | @PostConstruct |
| | | public void getFile() { |
| | | FILE_PATH = this.file; |
| | | } |
| | | |
| | | @PostConstruct |
| | | public void getWordUrl(){ |
| | | WORD_URL_PATH = this.wordUrl; |
| | | } |
| | | |
| | | @PostConstruct |
| | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 保存文件到word文件夹里 |
| | | * @param file |
| | | * @return |
| | | */ |
| | | public static String uploadWordFile(MultipartFile file) { |
| | | String urlString; |
| | | String pathName; |
| | | String path; |
| | | try { |
| | | String contentType = file.getContentType(); |
| | | if (contentType != null && contentType.startsWith("image/")) { |
| | | // 是图片 |
| | | path = FILE_PATH; |
| | | } else { |
| | | // 是文件 |
| | | path = WORD_URL_PATH; |
| | | } |
| | | File realpath = new File(path); |
| | | if (!realpath.exists()) { |
| | | realpath.mkdirs(); |
| | | } |
| | | pathName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmss")) + "-" + file.getOriginalFilename(); |
| | | urlString = realpath + "/" + pathName; |
| | | file.transferTo(new File(urlString)); |
| | | return pathName; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | } |