layui上传文件data后台怎么获取

本文环境:windows10、layui2.5.6,本文适用于所有品牌的电脑。
思路:
首先获取原始文件名你,并截取文件名后缀;然后重新命名新文件名;最后判断文件路径是否存在该文件,如果存在则将文件写入即可。
代码示例:
@PostMapping("/json/up")
public String uploadFujian2( MultipartFile file) {
System.out.println(file.getSize());
//readIoStringToFile(file.toString(),"D://test/test.doc");
if (file != null) {
try {
//String fileRealName = file.getOriginalFilename();//获得原始文件名;
//int pointIndex = fileRealName.lastIndexOf(".");//点号的位置
//String fileSuffix = fileRealName.substring(pointIndex);//截取文件后缀
//String fileNewName = DateUtils.getNowTimeForUpload();//新文件名,时间戳形式yyyyMMddHHmmssSSS
//String saveFileName = fileNewName.concat(fileSuffix);//新文件完整名(含后缀)
String filePath = "D:\\FileAll" ;
File path = new File(filePath); //判断文件路径下的文件夹是否存在,不存在则创建
if (!path.exists()) {
path.mkdirs();
}
File savedFile = new File(filePath);
savedFile = new File(filePath,"test.doc");
// 使用下面的jar包
FileUtils.copyInputStreamToFile(file.getInputStream(),savedFile);
boolean isCreateSuccess = savedFile.createNewFile(); // 是否创建文件成功
if(isCreateSuccess){ //将文件写入
//第一种
//file.transferTo(savedFile);
//第二种
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
System.out.println("文件是空的");
}
//IOUtils.read(file)
//File dest = new File("");
return "ok";
}
标签: