上传功能
在自己写的借助于“若依”框架的springboot项目中,写上传下载功能,想记录一下,所以有了此篇文章。
1. 配置文件filePath.properties
##文件上传路径
// 定义文件上传路径 D:\\ 此为路径,可根据自己需求修改
file.fileUploadPath=D:\\AA\\bankCardService
2. 实体类
##定义上传FileUploadProp实体类
package com.cn.web.prop;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "file")
@PropertySource(value = "filePath.properties")
public class FileUploadProp {
private String fileUploadPath;
public String getFileUploadPath() {
return fileUploadPath;
}
public void setFileUploadPath(String fileUploadPath) {
this.fileUploadPath = fileUploadPath;
}
}
3. 前端写法
##文件上传前端界面upload.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head>
<th:block th:include="include :: header('【请填写功能名称】列表')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-appendix-add" enctype="multipart/form-data">
<input type="hidden" name="appendixRelevanceid" id="appendixRelevanceid" class="form-control" th:value="${arId}">
<div class="form-group">
<label class="col-sm-3 control-label">文件:</label>
<div class="col-sm-8">
<input name="file" id="file" class="form-control" type="file">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">说明:</label>
<div class="col-sm-8">
<input type="text" name="appendixRemark" id="appendixRemark" class="form-control">
</div>
</div>
</form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
const prefix = ctx + "web/targetAppendix";
function submitHandler() {
uploadFile();
}
//上传方法
function uploadFile(){
// debugger;
var formData = new FormData();
if ($('#file')[0].files[0] == null) {
$.modal.alertWarning("请先选择文件");
return false;
}
//参数
formData.append('appendixRelevanceid', $("#appendixRelevanceid").val());
formData.append('appendixRemark', $("#appendixRemark").val());
//文件
formData.append('file', $('#file')[0].files[0]);
//上传
$.ajax({
url: prefix + "/addAppendix",
type: 'post',
cache: false,
data: formData,
processData: false,
contentType: false,
dataType: "json",
success: function(result) {
$.operate.successCallback(result);
}
});
}
</script>
</body>
</html>
3. 后端上传写法
xxController.java
// 文件上传保存
@PostMapping("/addAppendix")
@ResponseBody
public AjaxResult addAppendix(@RequestParam("file") MultipartFile file, TargetAppendix targetAppendix) {
System.out.println("资源查看附件"+fileUploadProp.getFileUploadPath());
if(file == null){
return AjaxResult.error("文件不允许为空");
}
//文件名
String fileName = file.getOriginalFilename();
//文件后缀名
String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
//新文件名
String newFileName = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
//拿到路径
String realPath = fileUploadProp.getFileUploadPath();
//文件夹不存在的时候创建一个
File dir = new File(realPath);
if(!dir.exists()){
dir.mkdirs();
}
String transFerName = newFileName+"."+suffixName;
//创建file对象,新地址新名字
File newFile = new File(realPath, transFerName);
try {
file.transferTo(newFile);
} catch (IOException e) {
e.printStackTrace();
return AjaxResult.error("文件上传失败");
}
String id = TargetUtils.getUUID();
targetAppendix.setId(id);
targetAppendix.setAppendixName(fileName);
targetAppendix.setAppendixHidename(transFerName);
targetAppendix.setAppendixUrl(realPath+"\\"+transFerName);
//获取当前登陆人
SysUser sysUser = ShiroUtils.getSysUser();
targetAppendix.setCreateId(String.valueOf(sysUser.getUserId()));
targetAppendix.setCreateBy(sysUser.getLoginName());
targetAppendix.setCreateTime(TargetUtils.getDate());
return toAjax(targetAppendixService.insertTargetAppendix(targetAppendix));
}

希望对您有所帮助!
1482



被折叠的 条评论
为什么被折叠?



