使用SpringBoot+Vue+Antd 编写上传文件功能
前端:使用Vue组件库
Select File
{{ uploading ? 'Uploading' : 'Start Upload' }}
后端:使用SpringBoot
Controller层使用file接收
所以前端请求的时候也要用file
public ResponseEntitycreateModelTrain(@RequestParam("qdtzCode") String qdtzCode, @RequestParam("file") MultipartFile file) throws IOException { InputStream inputStream = file.getInputStream(); ModelTrainingAnnex modelTrainingAnnex = StorageUtil.touch(inputStream, qdtzCode, file.getOriginalFilename(), false); return null; }
public static ModelTrainingAnnex touch(InputStream source, String qdtzCode, String targetPath, Boolean ignoreSuffix) {
// 文件流是否存在
if (source == null) {
log.error("文件流为 null !");
throw new RuntimeException("文件流为 null !");
}
// 处理文件路径
targetPath = targetPathHandle(targetPath);
if (targetPath == null) {
log.error("未拼接文件名");
throw new RuntimeException("未拼接文件名");
}
// 文件名
String name = FileNameUtil.getName(targetPath);
String suffix = FileNameUtil.getSuffix(targetPath);
if (!name.contains(".") && !ignoreSuffix) {
log.error("文件无后缀名");
throw new RuntimeException("文件无后缀名");
}
// 保存文件的绝对路径
String path = StrUtil.format("{}{}{}.{}", GLOBAL_ROOT_PATH, qdtzCode + "\", targetPath.replace(name, String.valueOf(IdUtil.getSnowflakeNextId())), suffix);
// 保存文件
OutputStream target = FileUtil.getOutputStream(FileUtil.touch(path));
Long fileSize = 0L;
try {
fileSize = NioUtil.copyByNIO(source, target, NioUtil.DEFAULT_MIDDLE_BUFFER_SIZE, null) / 1024;
} catch (Exception e) {
log.error("保存文件异常:", e);
} finally {
IoUtil.close(target);
}
// 最大排序号
ModelTrainingAnnex maxSeq = modelTrainingAnnexRepository.findTopByOrderBySequenceDesc();
// 当前登录用户
Optional userName = SecurityUtils.getCurrentUserName();
// 保存到数据库
ModelTrainingAnnex ModelTrainingAnnex = null;
try {
ModelTrainingAnnex = new ModelTrainingAnnex();
ModelTrainingAnnex.setId(IdUtil.simpleUUID().toUpperCase(Locale.ROOT));
ModelTrainingAnnex.setName(name);
ModelTrainingAnnex.setFileType(suffix);
ModelTrainingAnnex.setFilePath(path);
ModelTrainingAnnex.setFileSize(fileSize);
ModelTrainingAnnex.setIsInternetUrl(false);
ModelTrainingAnnex.setInternetiUrlPrefix(null);
ModelTrainingAnnex.setUploadTime(DateUtil.toInstant(new Date()));
ModelTrainingAnnex.setCreator(userName.orElse(null));
ModelTrainingAnnex.setCreatedTime(DateUtil.toInstant(new Date()));
ModelTrainingAnnex.setModifiedTime(DateUtil.toInstant(new Date()));
ModelTrainingAnnex.setSequence(maxSeq == null ? 0 : maxSeq.getSequence() + 1);
ModelTrainingAnnex.setQdtzCode(qdtzCode);
if (suffix.equals("jsonl")) {
ModelTrainingAnnex.setModelState("0");
} else if (suffix.equals("xls") || suffix.equals("xlsx")) {
ModelTrainingAnnex.setModelState("3");
}
modelTrainingAnnexRepository.save(ModelTrainingAnnex);
} catch (Exception e) {
ModelTrainingAnnex = null;
// 删除文件
rm(path);
log.error("保存到数据库异常,删除文件", e);
}
return ModelTrainingAnnex;
}



