工具类涉及pom依赖包
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
工具类
/**
* SpringBoot上传文件工具类
* @author LinkinStar
*/
public class FileHandleUtil {
/** 绝对路径 **/
private static String absolutePath = "";
/** 静态目录 **/
private static String staticDir = "static";
/** 文件存放的目录 **/
private static String fileDir = "/upload/";
/**
* 上传单个文件
* 最后文件存放路径为:static/upload/image/test.jpg
* 文件访问路径为:http://127.0.0.1:8080/upload/image/test.jpg
* 该方法返回值为:/upload/image/test.jpg
* @param inputStream 文件流
* @param path 文件路径,如:image/
* @param filename 文件名,如:test.jpg
* @return 成功:上传后的文件访问路径,失败返回:null
*/
public static String upload(InputStream inputStream, String path, String filename) {
//第一次会创建文件夹
createDirIfNotExists();
String resultPath = fileDir + path + filename;
//存文件
File uploadFile = new File(absolutePath, staticDir + resultPath);
try {
FileUtils.copyInputStreamToFile(inputStream, uploadFile);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return resultPath;
}
/**
* 创建文件夹路径
*/
private static void createDirIfNotExists() {
if (!absolutePath.isEmpty()) {return;}
//获取跟目录
File file = null;
try {
file = new File(ResourceUtils.getURL("classpath:").getPath());
} catch (FileNotFoundException e) {
throw new RuntimeException("获取根目录失败,无法创建上传目录!");
}
if(!file.exists()) {
file = new File("");
}
absolutePath = file.getAbsolutePath();
File upload = new File(absolutePath, staticDir + fileDir);
if(!upload.exists()) {
upload.mkdirs();
}
}
/**
* 删除文件
* @param path 文件访问的路径upload开始 如: /upload/image/test.jpg
* @return true 删除成功; false 删除失败
*/
public static boolean delete(String path) {
File file = new File(absolutePath, staticDir + path);
if (file.exists()) {
return file.delete();
}
return false;
}
}
@RequestMapping("/upload")
@ResponseBody
public ResponseData upload(HttpServletRequest requet, @ModelAttribute MultipartFile file ) throws IllegalStateException, IOException {
if(!file.isEmpty()) {
String result = requet.getScheme()+"://"+requet.getServerName()+":"+requet.getServerPort() + FileHandleUtil.upload(file.getInputStream(), "file/", file.getOriginalFilename());
return new ResponseData("0", "上传成功", result);
}else {
return new ResponseData("1", "上传失败", null);
}
}
application.properties 文件记得加上如下配置,使jar包运行时能正常访问到上传文件。
spring.resources.static-locations=classpath:static/,file:static/
一、本地项目编译运行,上传后位置如图:
文件访问路径: http://localhost:8099/upload/file/数据共享管理办法_初稿.pdf
二、jar包运行,上传文件位置如下图:
文件访问路径: http://localhost:8099/upload/file/数据共享管理办法_初稿.pdf