
package cn.daenx.demo.controller;
import cn.daenx.demo.Aoidj;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;
/**
* 文件上传的一些案例
*/
@RestController
@RequestMapping("/test")
public class TestController {
@PostMapping(value = "/02")
@ResponseBody
private String req0122(@ModelAttribute Aoidj aoidj,@RequestPart(value = "files",required = false) List<MultipartFile> files) throws Exception {
System.out.println(aoidj.getName());
System.out.println(aoidj.getAge());
//files或者aoidj.getFiles()都可以拿到值
for (MultipartFile file : files) {
BigDecimal fileSize = getFileSize(file, 2);
if (fileSize.compareTo(new BigDecimal("50")) > 0) {
throw new RuntimeException("文件大小最多支持50MB");
}
String fileName = file.getOriginalFilename();
if (ObjectUtil.isEmpty(fileName)) {
throw new RuntimeException("文件名为空");
}
String md5 = getMd5(file);
String fileId = md5 + getPrefix(file);
String FILE_PATH = "D:/home/phcp/file";
String path = FILE_PATH + "/test/" + fileId;
handFilePath(path);
File file1 = new File(path);
if (!file1.exists()) {
try {
file.transferTo(file1);
} catch (IOException e) {
throw new RuntimeException("保存文件失败");
}
}
}
return "ok";
}
/**
* 判断多级路径是否存在,不存在就创建
*
* @param filePath 支持带文件名的Path:如:D:/anxinsign/test1/abc/ttt/a.file,和不带文件名的Path:如:D:/anxinsign/test2/abcaa/ttt2
*/
public static void handFilePath(String filePath) {
String paths[] = {""};
//切割路径
try {
String tempPath = new File(filePath).getCanonicalPath();//File对象转换为标准路径并进行切割,有两种windows和linux
paths = tempPath.split("\\\\");//windows
if (paths.length == 1) {
paths = tempPath.split("/");
}//linux
} catch (IOException e) {
System.out.println("切割路径错误");
}
//判断是否有后缀
boolean hasType = false;
if (paths.length > 0) {
String tempPath = paths[paths.length - 1];
if (tempPath.length() > 0) {
if (tempPath.indexOf(".") > 0) {
hasType = true;
}
}
}
//创建文件夹
String dir = paths[0];
for (int i = 0; i < paths.length - (hasType ? 2 : 1); i++) {// 注意此处循环的长度,有后缀的就是文件路径,没有则文件夹路径
try {
dir = dir + "/" + paths[i + 1];//采用linux下的标准写法进行拼接,由于windows可以识别这样的路径,所以这里采用警容的写法
File dirFile = new File(dir);
if (!dirFile.exists()) {
dirFile.mkdir();
System.out.println("成功创建目录:" + dirFile.getCanonicalFile());
}
} catch (Exception e) {
System.err.println("文件夹创建发生异常");
}
}
}
/**
* 获取上传文件的后缀,例如 .png
*
* @param multipartFile
* @return
* @throws Exception
*/
public static String getPrefix(MultipartFile multipartFile) {
//获取文件名,例如:QQ截图.png
String originalFilename = multipartFile.getOriginalFilename();
//获取文件后缀,例如:.png
String prefix = originalFilename.substring(originalFilename.lastIndexOf("."));
return prefix;
}
/**
* 获取上传文件的md5
* 32位小写
*
* @param multipartFile
* @return
* @throws Exception
*/
public static String getMd5(MultipartFile multipartFile) throws Exception {
//获取文件的byte信息
byte[] uploadBytes = multipartFile.getBytes();
//拿到一个MD5转换器
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(uploadBytes);
//转换为16进制
return new BigInteger(1, digest).toString(16);
}
/**
* 获取文件大小
*
* @param file
* @param type 单位,1=KB,2=MB
* @return
*/
public static BigDecimal getFileSize(MultipartFile file, int type) {
long size = file.getSize();
BigDecimal sizeDecimal = new BigDecimal(size);
BigDecimal kbDecimal = new BigDecimal(type == 1 ? "1024" : "1048576");
BigDecimal sizeKB = sizeDecimal.divide(kbDecimal, 2, BigDecimal.ROUND_HALF_UP);
return sizeKB;
}
/**
* 获取文件大小
*
* @param file
* @param type 单位,1=KB,2=MB
* @return
*/
public static BigDecimal getFileSize(File file, int type) {
long size = file.length();
BigDecimal sizeDecimal = new BigDecimal(size);
BigDecimal kbDecimal = new BigDecimal(type == 1 ? "1024" : "1048576");
BigDecimal sizeKB = sizeDecimal.divide(kbDecimal, 2, BigDecimal.ROUND_HALF_UP);
return sizeKB;
}
}