nginx代理图片和视频
首先官网下载nginx,以window为例,如图所
配置nginx.conf
在该路径下放一张图片
启动nginx
游览器中打开
如果看到图片后,恭喜nginx配置成功了,哈哈。。。
下面就是上传java代码 `
/**
* @Description 返回数据
* @Created huangW
* @Date 2020/12/8 9:58
* @Version 1.0
*/
public class R extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
public R() {
put("code", 200);
put("msg", "success");
}
public static R error() {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员",new HashMap<>());
}
public static R error(String msg) {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg,new HashMap<>());
}
public static R error(int code, String msg, Map<String,Object> data) {
R r = new R();
r.put("code", code);
r.put("msg", msg);
r.put("data",data);
return r;
}
public static R ok(String msg) {
R r = new R();
r.put("msg", msg);
return r;
}
public static R ok(Map<String, Object> map) {
R r = new R();
r.putAll(map);
return r;
}
public static R ok() {
return new R();
}
public R put(String key, Object value) {
super.put(key, value);
return this;
}
}
/**
* @Description 文件上传
* @Created huangW
* @Date 2020/12/8 9:58
* @Version 1.0
*/
@RestController
public class UploadController {
@Autowired
private UploadService uploadService;
@PostMapping("/upload")
public R upload(@RequestParam("file") MultipartFile file) {
return uploadService.upload(file);
}
}
/**
* @Description 文件上传service
* @Created huangW
* @Date 2020/12/8 10:00
* @Version 1.0
*/
@Service
@Slf4j
public class UploadService {
@Autowired
private UploadConfig uploadConfig;
/**
* 文件上传
* @param file
* @return
*/
public R upload(MultipartFile file) {
if (file.getSize() > 1024 * 1024 * 50) {
return R.error("文件太大");
}
//获取绝对路径
String realPath = uploadConfig.getRootPath();
//文件存放的目录
File folder = new File(realPath);
if (!folder.isDirectory()) {
folder.mkdirs();
}
String oldName = file.getOriginalFilename();
if(StringUtils.isBlank(oldName)){
return R.error("文件为空");
}
//文件后缀
String suffix = oldName.substring(oldName.lastIndexOf("."));
if (checkImg(suffix)) {
return R.error("类型错误");
}
//文件新名字
String newName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
String filePath = null;
try {
File targetFile = new File(folder, newName);
if (!targetFile.exists()) {
targetFile.mkdirs();
} else {
targetFile.delete();
}
file.transferTo(targetFile);
filePath = uploadConfig.getImgHttp() + newName;
} catch (IOException e) {
e.printStackTrace();
return R.error("上传失败");
}
return R.ok(filePath);
}
/**
* 检查文件类型
*
* @param suffix
* @return
*/
private boolean checkImg(String suffix) {
List<String> list = Arrays.asList(".bmp", ".gif", ".jpg", ".jpeg", ".png",".mp4");
for (String s : list) {
if (StringUtils.equalsIgnoreCase(s, suffix)) {
return false;
}
}
return true;
}
}
/**
* @Description 上传配置
* @Created huangW
* @Date 2020/12/8 9:58
* @Version 1.0
*/
@Data
@Component
@ConfigurationProperties(prefix = "upload-config")
public class UploadConfig {
private String rootPath;
private String imgHttp;
}
yml配置
upload-config:
#附件存储根目录
rootPath: D:\picture\pic
imgHttp: http://localhost:8000/pic/
接口测试结果 图片上传
视频MP4格式