大概所需
1.配置上传文件/图片/视频的前半段路径
2.工具类:上传时get到配置文件uploadUrl.properties中的url配置
package com.nz.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
* @Author xxs
* @Date 2020/4/20 23:49
*/
public class GetPropertise {
Properties p = null;
public GetPropertise(String path) {
FileInputStream in;
try {
in = new FileInputStream(new File(path));
p = new Properties();
p.load(in);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getProperty(String key) {
if(p == null) {
return null;
}
String property;
try {
property = p.getProperty(key);
// System.out.println( property);
} catch (Exception e) {
return null;
}
return property;
}
public String getProperty(String key, String value) {
if(p == null) {
return null;
}
if (p.getProperty(key) == null) {
return value;
}
return p.getProperty(key);
}
}
package com.nz.util;
/**
* @Author xxs
* @Date 2020/4/20 23:55
*/
public class GetUploadUrl {
static GetPropertise getPropertise = null ;
static {
// "/usr/OfficialWebsite/uploadUrl.properties"
getPropertise = new GetPropertise("这里放uploadUrl.properties资源文件的路径");
}
public static final String uploadUrl = getPropertise.getProperty("uploadUrl");
}
3.上传文件接口方法
package com.nz.controller;
import com.nz.util.GetPropertise;
import com.nz.util.GetUploadUrl;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @Author xxs
* @Date 2020/4/20 18:59
*/
@RestController
@ResponseBody
@RequestMapping("/upload")
public class uploadFileController {
@RequestMapping("/fileUpload") // 等价于 @RequestMapping(value = "/upload", method = RequestMethod.POST)
public String fileUpload(HttpServletRequest req, @RequestParam("file") MultipartFile file) {
//1. 接收上传的文件 @RequestParam("file") MultipartFile file
//2.根据时间戳创建新的文件名,这样即便是第二次上传相同名称的文件,也不会把第一次的文件覆盖了
String fileName = System.currentTimeMillis() + file.getOriginalFilename();
//3.通过req.getServletContext().getRealPath("") 获取当前项目的真实路径,然后拼接前面的文件名
// String destFileName = req.getServletContext().getRealPath("") + "uploaded" + File.separator + fileName;
// String realPathDir = req.getSession().getServletContext().getRealPath("/");
String realPathDir = GetUploadUrl.uploadUrl;
String path = "/static/OfficialWebsite/upload/"+fileName;
System.out.println("文件路径"+realPathDir+path);
try {
//4.第一次运行的时候,这个文件所在的目录往往是不存在的,这里需要创建一下目录(创建到了webapp下uploaded文件夹下)
File destFile = new File(realPathDir+path);
destFile.getParentFile().mkdirs();
//5.把浏览器上传的文件复制到希望的位置
file.transferTo(destFile);
System.out.println(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return "上传失败," + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "上传失败," + e.getMessage();
}
return path;
}
}
4.上传文件页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload/fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<br>
<input type="submit" value="上传">
</form>
</body>
</html>
另外,配置文件application.properties要配置一些文件的限制:
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=200MB
spring.mvc.static-path-pattern=/static/**
运行后:访问上传文件页面
上传后返回要返回数据库的后半段路径
看一下我控制台的打印
我们可以拿这个全路径看一下,可以正常访问
当然了,此视频文件是存在了项目文件夹下: