UuidUtil.java
package com.mracale.util;import java.util.UUID;public class UuidUtil {public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f","g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s","t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5","6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V","W", "X", "Y", "Z" };/*** 获取短UUID* @return*/public static String getShortUuid() {StringBuffer shortBuffer = new StringBuffer();String uuid = UuidUtil.getUuid();for (int i = 0; i < 8; i++) {String str = uuid.substring(i * 4, i * 4 + 4);int x = Integer.parseInt(str, 16);shortBuffer.append(chars[x % 0x3E]); // 对62取余}return shortBuffer.toString();}/*** 获得32位UUID*/public static String getUuid(){String uuid = UUID.randomUUID().toString();//去掉“-”符号return uuid.replaceAll("-", "");}public static void main(String[] args) {System.out.println(getShortUuid());}}UploadController.java 控制器,接收前端文件上传
package com.mracale.controller;import com.mracale.dto.FileDto;import com.mracale.dto.ResponseDto;import com.mracale.util.Base64ToMultipartFile;import com.mracale.util.UuidUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import java.io.*;import java.text.SimpleDateFormat;import java.util.Date;@RequestMapping("/admin")@RestControllerpublic class UploadController {private static final Logger LOG = LoggerFactory.getLogger(UploadController.class);private static final String storePath = "D:/phpstudy_pro/WWW/java/springboot-test/file/";@RequestMapping("/upload1")public ResponseDto upload(@RequestParam MultipartFile file) throws Exception {LOG.info("上传文件开始:{}", file);LOG.info(file.getOriginalFilename());LOG.info(String.valueOf(file.getSize()));// 保存文件到本地String fileName = file.getOriginalFilename();String key = UuidUtil.getShortUuid();Date date = new Date();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/");String format = simpleDateFormat.format(date);LOG.info("日期信息:"+format);String fullPath = storePath + format;if (!new File(fullPath).exists()){new File(fullPath).mkdirs();}fullPath = fullPath + key + "-" + fileName;File dest = new File(fullPath);try{file.transferTo(dest);}catch (IOException e){e.printStackTrace();}LOG.info(dest.getAbsolutePath());ResponseDto responseDto = new ResponseDto();responseDto.setContent("http://127.0.0.1:8080/f/"+ format + key + "-" + fileName);return responseDto;}@RequestMapping("/upload")public ResponseDto upload(@RequestBody FileDto fileDto) throws Exception {LOG.info("上传文件开始");String use = fileDto.getUse();String key = fileDto.getKey();String suffix = fileDto.getSuffix();String shardBase64 = fileDto.getShard();MultipartFile shard = Base64ToMultipartFile.base64ToMultipart(shardBase64);// 保存文件到本地Date date = new Date();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/");String format = simpleDateFormat.format(date);LOG.info("日期信息:"+format);String fullPath = storePath + format;if (!new File(fullPath).exists()){new File(fullPath).mkdirs();}String path = new StringBuffer(fullPath).append(key).append(".").append(suffix).toString(); // course6sfSqfOwzmik4A4icMYuUe.mp4String localPath = new StringBuffer(path).append(".").append(fileDto.getShardIndex()).toString(); // course6sfSqfOwzmik4A4icMYuUe.mp4.1File dest = new File(localPath);shard.transferTo(dest);LOG.info(dest.getAbsolutePath());LOG.info("保存文件记录开始");fileDto.setPath(path);// 合并分片if (fileDto.getShardIndex().equals(fileDto.getShardTotal())) {this.merge(fileDto);}ResponseDto<Object> responseDto = new ResponseDto<>();return responseDto;}public void merge(FileDto fileDto) throws Exception {LOG.info("合并分片开始");String path = fileDto.getPath(); //http://127.0.0.1:9000/file/f/course6sfSqfOwzmik4A4icMYuUe.mp4Integer shardTotal = fileDto.getShardTotal();File newFile = new File(path);FileOutputStream outputStream = new FileOutputStream(newFile, true);//文件追加写入FileInputStream fileInputStream = null;//分片文件byte[] byt = new byte[10 * 1024 * 1024];int len;try {for (int i = 0; i < shardTotal; i++) {// 读取第i个分片fileInputStream = new FileInputStream(new File( path + "." + (i + 1))); //course6sfSqfOwzmik4A4icMYuUe.mp4.1while ((len = fileInputStream.read(byt)) != -1) {outputStream.write(byt, 0, len);}}} catch (IOException e) {LOG.error("分片合并异常", e);} finally {try {if (fileInputStream != null) {fileInputStream.close();}outputStream.close();LOG.info("IO流关闭");} catch (Exception e) {LOG.error("IO流关闭", e);}}LOG.info("合并分片结束");// 删除分片LOG.info("删除分片开始");File file = null;String filePath = "";for (int i = 0; i < shardTotal; i++) {filePath =path + "." + (i + 1);file = new File(filePath);System.gc();boolean result = file.delete();LOG.info("删除{},{}", filePath, result ? "成功" : "失败");}LOG.info("删除分片结束");}}
推荐阅读
- MyBatis 使用数组作为参数,这里藏着一个大坑
- php大文件切片上传
- Aspose-文件转换工具太好用了
- 一点隐私都没有了?聊天记录打码也不安全:大神亲自下场破解
- 轻量、高效、功能强大的微前端框架-MicroApp
- 小庄园的红茶,翠玉茶的功效与作用
- 产后大出血症状
- hr|大学生就业屡屡失败,大企业HR坦言:这些简历不用细看直接淘汰
- 00后|00后的简历有多敢写?表面看着很正经,括号里的字却让HR头大
- 大学|大学毕业生如何提高沟通表达能力?
