介绍
原理其实就是让Spring Boot直接访问本地静态资源,类似于 nginx 吧,nginx还得配置,Spring Boot加个配制就行
配置
第一种方式
application.xml中加入如下配置
static-locations为本地资源路径
linux以/表示根目录,windows下可以用file:D来映射
spring:
mvc :
static-path-pattern: /**
resources:
static-locations: file:D:/image/
这样直接通过浏览器就能访问我D盘下面image文件中的内容,放点图片,视频,pdf
搭建一个图片,视频,pdf服务器就是这么简单
第二种方式
和上面其实差不多,配置方式改为java,图片方便大家看,代码方便大家粘贴
@Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("file:D:/image/"); }
}
来看效果
我把静态资源都放在D:/image这个目录下
各种本地资源都能展示
图片
视频
为了方便大家上传资源,我连上传代码也给大家准备好了
上传资源代码
想要让spring boot项目支持显示 html 页面,先加入如下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
上传文件的controller,用Controller注解,跳转到testload.html页面
@Controller public class TestUploadFile { @RequestMapping(value = "testupload", method = RequestMethod.GET) public String testUpload() { return "testload"; } }
在templates目录下放testload.html
<!DOCTYPE html> <html lang="zn"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> springmvc上传文件 <form name="form1" action="/wx/user/uploadImage" method="post" enctype="multipart/form-data"> <input type="file" name="upload_file" /> <input type="submit" value="springboot上传文件" /> </form> </body> </html>
页面效果如下,可以美化一下哈
controller写成如下形式
@PostMapping(value = "uploadImage") public ServerResponse upload(@RequestParam(value = "upload_file", required = false)MultipartFile file) { }
上传代码如下:
// 第一个参数文件,第二个参数路径
@Override
public String upload(MultipartFile file, String path) {
// 得到上传的文件名
String fileName = file.getOriginalFilename();
// 文件到扩展名
String fileExtensionName = fileName.substring(fileName.lastIndexOf(".") + 1);
// 重新生成fileName是为了防止覆盖
String uploadFileName = UUID.randomUUID().toString() + "." + fileExtensionName;
//{}是占位符
log.info("start upload file,old fileName: {},upload path: {},new fileName: {}", fileName, path, uploadFileName);
//创建所需的文件
File fileDir = new File(path);
if (! fileDir. exists ()) {
fileDir.setWritable(true);
//创建该目录下的抽象路径名命名,包括任何必要的但不存在父目录。
fileDir.mkdirs();
}
File targetFile = new File(path, uploadFileName);
try {
file.transferTo(targetFile);
} catch (IOException e) {
log.error("image upload fail", e);
return null;
}
return "上传成功";
}
8USD bottle Clobex Spray 0 These findings suggest a role for endogenous estrogens in radiation related breast carcinogenesis, making tamoxifen a viable risk reducing option for pre and post menopausal cancer survivors
Biotinylation of PCEC membrane protein in mice was achieved by pulmonary perfusion through the pulmonary artery 53
If an infant is to be discharged to a home where illicit substances were used, a home visit before discharge and many home visits after discharge are necessary to protect the infant s health and well being
The teratogenic effects of alcohol use during pregnancy are well documented 16
ly 31sIdv8 pics of nakeed moms granny aand young girl sex pictures
Use NPP in conjunction with other supplements