1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| public class FileUploadUtils {
public static String fileMerge(String parentPath, String fileMD5, String fileName, int chunkNum) { String finalFileName = null; FileOutputStream targetFile = null; try { new File(filePathCreate(parentPath, fileMD5)).mkdirs(); finalFileName = filePathCreate(parentPath, fileMD5, fileName); log.info("finalFileName : {}", finalFileName); targetFile = new FileOutputStream(finalFileName); byte[] buffer = new byte[1024]; for (int i = 1; i <= chunkNum; i ++ ) { String chunkFile = i + ".tmp"; File tmpFile = new File(filePathCreate(parentPath, fileName, chunkFile)); InputStream inputStream = new FileInputStream(tmpFile); int len; while ((len = inputStream.read(buffer)) != -1) { targetFile.write(buffer, 0, len); } inputStream.close(); } } catch (FileNotFoundException e) { log.error("File merge FileNotFoundException : {}", e.getMessage()); finalFileName = "Err"; } catch (IOException e) { log.error("File merge IOException : {}", e.getMessage()); } finally { if (targetFile != null) { try { targetFile.close(); } catch (IOException e) { log.error("File merge IOException : {}", e.getMessage()); } } } return finalFileName; }
public static ResultData<HttpStatus> saveFile(FilePart uploadFile, String parentPath, String name) { if (uploadFile == null || StringUtils.isNotEmpty(name)) { return ResultData.builder().value(HttpStatus.CONFLICT).errMsg("上传失败").builded(); } new File(parentPath).mkdirs(); String fileName = parentPath + name; File file = new File(fileName); if (file.exists()) { return ResultData.builder().value(HttpStatus.CONFLICT).errMsg("文件已存在").builded(); } uploadFile.transferTo(file); return ResultData.builder().value(HttpStatus.OK).builded(); }
public static boolean judgeFileExist(String parentPath, String fileName) { return new File(parentPath + fileName).exists(); }
public static String filePathCreate(String parent, String present, String... other) { StringBuilder sb = new StringBuilder(); sb.append(parent).append(present).append("/"); for (String s : other) { sb.append(s).append("/"); } return sb.toString(); } }
|