1、application
#文件上传
#enabled:是否支持上传,默认是ture/maxFileSize:单个文件的最大值/maxRequestSize:上传文件总的最大值/location:上传文件的位置/如果不限制大小,则设置为-1即可
spring.servlet.multipart.enabled=true
spring.servlet.multipart.maxFileSize=10MB
spring.servlet.multipart.maxRequestSize=-1
#spring.servlet.multipart.location=classpath:/static/
2、FileUtil.java
1、上传
package com.demo.utils;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class FileUtil {
private final static Logger loggin = LoggerFactory.getLogger(FileUtil.class);
//上传路径
private final static String top = "C:\\wwwroot\\";
//System.getProperty("user.dir")//项目路径
//上传 文件、路径
private String upload(MultipartFile file, String path) {
String currpicture = null;
try {
if (file != null && file.getSize() > 0) {
String fileName = file.getOriginalFilename();
//新文件名称
//文件的类型
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
//时间格式年月日时分秒毫
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssS");
String time = simpleDateFormat.format(System.currentTimeMillis());
currpicture = time + "." + suffix;
//存储地址
String destFileName = top + path + currpicture;
File destFile = new File(destFileName);
destFile.getParentFile().mkdirs();
file.transferTo(destFile);
}
} catch (IOException e) {
loggin.info("文件上传失败:" + file.getOriginalFilename());
return null;
}
return path + currpicture;
}
//删除
public Boolean delect(String path) {
File destFile = new File(top + path);
return destFile.delete();
}
}
注:务必从新从数据库拿,博主感觉是缓存问题,也解释不清楚
2、删除
@RequestMapping("delect")
public String delect(){
String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\";
String img = "20190108153155294.jpg";
File destFile = new File(path+img);
destFile.delete();
System.out.println("***");
return "file";
}
3、界面自己写一下,博主偷一下懒
注:返回前端界面可以不带static,但是如果不启动项目必须要和前端书写路劲相同,根据自己情况分析