接口
@Controller
@ResponseBody
@RequestMapping(value = "/file")
public class UploadController {
/**
* 上传地址
*/
@Value("${file.upload.path}")
private String filePath;
// 执行上传
@RequestMapping("/upload")
public TitResult upload(MultipartFile file) {
String filename = "";
// String root="file:///D:/Java/langmu/src/main/resources/static";
String rootPath = "static/rotPhoto/";
// 获取上传文件名
filename = file.getOriginalFilename();
String suffix = filename.substring(filename.lastIndexOf("."));
//修改文件名
String newFileName = getUUID()+suffix;
// 定义上传文件保存路径
String path = filePath + rootPath;
// 新建文件
File filepath = new File(path, newFileName);
// 判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
try {
// 写入文件
file.transferTo(new File(path + newFileName));
} catch (IOException e) {
e.printStackTrace();
}
// 将src路径发送至html页面
return TitResult.success(rootPath + newFileName);
}
private String getUUID() {
UUID uuid = UUID.randomUUID();
String uuidStr = uuid.toString();
return uuidStr;
}
}
配置文件
/**
* 资源映射路径
*/
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
/**上传地址*/
@Value("${file.upload.path}")
private String filePath;
/**显示相对地址*/
@Value("${file.upload.path.relative}")
private String fileRelativePath;
//设置静态文件的目录
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
//注意此处的/static/rotPhoto/和页面上显示的文件路径名保持一致
registry.addResourceHandler("/static/rotPhoto/**").addResourceLocations("file:D:\\Java\\langmu\\src\\main\\resources\\static\\rotPhoto\\");
}
}
application.properties
# 上传文件总的最大值
spring.servlet.multipart.max-request-size=1000MB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB
file.upload.path=src/main/resources/
file.upload.path.relative=/rotPhoto/**
#开启文件上传支持
spring.servlet.multipart.enabled=true
#文件写入磁盘阈值
spring.servlet.multipart.file-size-threshold=0
#上传文件临时保存位置
spring.servlet.multipart.location=src/main/resources/
#文件是否延迟解析false
spring.servlet.multipart.resolve-lazily=false
spring.resources.static-locations=classpath:/static/
pom
<!--文件上传-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
HTML页面
<form id="postForm">
<input type="file" name="file" id="picture">
<input type="button" value="上传" class="btn btn-success" @click="upload()"
style="width: 10%;padding: 0.3em 0;background: white;font-color: black;color: black;border: 1.5px solid;">
</form>
js
//上传文件
upload: function () {
var formData = new FormData();
formData.append('file', $('#picture')[0].files[0]);
var _this = this
$.ajax({
type: 'POST',
url: "/file/upload",
dataType: "formData",
data: formData,
contentType: false,//ajax上传图片需要添加
processData: false,//ajax上传图片需要添加
success: function (res) {
if (res.code == 0) {
_this.filePath = res.data
alert("上传成功")
} else {
alert(res.msg)
}
},
error: function (e) {
alert("error")
}
})
},