springboot怎么引入静态资源

springboot怎么引入静态资源,第1张

在SpringBoot中加载静态资源和在普通的web应用中不太一样。默认情况下,spring Boot从classpath下一个叫/static(/public,/resources或/META-INF/resources)的文件夹或从ServletContext根目录提供静态内容。下面我们来写拦数枝个例子看一下就会一目了然了:首先看一下项目的目录结毕腔构:

我们在resources下面的templates目录下建一个home.html的文件,完整目录为:src/main/resources/templates/home.html。内容如下:

<!DOCTYPE HTML>

<html xmlns:th="http://www.thymeleaf.org">

<head>

<简敏meta charset="utf-8"/>

<title>ConanZhang的首页</title>

SpringBoot我们大多数的时候是当做服务提供者来使用的,但是在一些场景中还是要用到一些文件上传下载这种"非常规" *** 作的。那么怎么在SpringBoot中实现文件的上传下载功能呢?想象一些我们在SpringMVC中是怎么做的。我们需要在SpringMVC的配置文件中增加文件上传的Bean的配置,如下:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

然后在后台对应的处理方法中就可以直接获取到文件的迹凯滑输入流了。而对于SpringBoot来说,我们不需要配置文件上传的解析类了,因为SpringBoot已经帮我们注册好了。下面我们来看一下具体的开发。

增加thymeleaf的依赖

这里我们用thymeleaf来作为页面的呈现,所以我们这里需要引入thymeleaf的相关依赖:

<dependency>

<groupId>org.springframework.boot</groupId>

<姿腊artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

文件上传下载页面:

接着我们需要写一个文件的上传下载的页面,我简单的写了下面这个页面

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head>

<meta charset="UTF-8"/>

<title>Title</title>

<script th:src="@{/js/jquery-2.1.4.min.js}"></script>

</head>

<body>

<form id="upload" enctype="multipart/form-data" method="post" action="/uploadAndDownload/uploadFileAction">

<input type="file" name="uploadFile"/>

<input type="hidden" name="id" value="12"/>

<input type="button" value="文件上传" onclick="doUpload()"/>

</form>

<input type="button" value="下载文件" onclick="doDownload()"/>

<form id="download" method="post" action="/uploadAndDownload/downloadFileAction">

</form>

<script type="text/javascript">

function doUpload() {

var upl = document.getElementById("upload")

upl.submit()

}

function doDownload() {

var upl = document.getElementById("download")

upl.submit()

}

</script>

</body>

</html>孙雀

后台处理类:

接着我们写一个处理文件上传和下载的控制类:

访问页面的方法:

@Controller

@RequestMapping("/uploadAndDownload")

public class UploadAndDownloadFileController {

@RequestMapping("/index")

public String index() {

return "uploadAndDownload"

}

}

上传文件的处理方法:

@RequestMapping(value = "/uploadFileAction", method = RequestMethod.POST)

public ModelAndView uploadFileAction(@RequestParam("uploadFile") MultipartFile uploadFile, @RequestParam("id") Long id) {

ModelAndView modelAndView = new ModelAndView()

modelAndView.setViewName("uploadAndDownload")

InputStream fis = null

OutputStream outputStream = null

try {

fis = uploadFile.getInputStream()

outputStream = new FileOutputStream("G:\\uploadfile\\"+uploadFile.getOriginalFilename())

IOUtils.copy(fis,outputStream)

modelAndView.addObject("sucess", "上传成功")

return modelAndView

} catch (IOException e) {

e.printStackTrace()

}finally {

if(fis != null){

try {

fis.close()

} catch (IOException e) {

e.printStackTrace()

}

}

if(outputStream != null){

try {

outputStream.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

modelAndView.addObject("sucess", "上传失败!")

return modelAndView

}

下载文件的处理方法:

@RequestMapping("downloadFileAction")

public void downloadFileAction(HttpServletRequest request, HttpServletResponse response) {

response.setCharacterEncoding(request.getCharacterEncoding())

response.setContentType("application/octet-stream")

FileInputStream fis = null

try {

File file = new File("G:\\config.ini")

fis = new FileInputStream(file)

response.setHeader("Content-Disposition", "attachmentfilename="+file.getName())

IOUtils.copy(fis,response.getOutputStream())

response.flushBuffer()

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

} finally {

if (fis != null) {

try {

fis.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

当我们发送请求为:http://localhost:8003/uploadAndDownload/index,会看到如下的页面(没有做排版处理):

当我们上传文件时,会调用uploadFileAction这个方法,然后将上传的文件信息存放到一个地方,根据个人的需求去做。

当我们下载文件时:

有时候我们可能需要限制上传文件的大小,可以这样设置上传文件的大小:

@Configuration

public class UploadFileConfiguration {

@Bean

public MultipartConfigElement multipartConfigElement() {

MultipartConfigFactory factory = new MultipartConfigFactory()

factory.setMaxFileSize("256KB")

factory.setMaxRequestSize("512KB")

return factory.createMultipartConfig()

}

}

有时候我们可能还要进行一些文件类型的现在,那么这个怎么做呢?我们可以通过自定的Interceptor来实现这样的功能。代码示例如下:

自定义的拦截器

package com.zkn.learnspringboot.aop

import javax.servlet.ServletOutputStream

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

import org.springframework.boot.context.properties.ConfigurationProperties

import org.springframework.stereotype.Component

import org.springframework.util.CollectionUtils

import org.springframework.util.StringUtils

import org.springframework.web.multipart.MultipartFile

import org.springframework.web.multipart.MultipartHttpServletRequest

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter

import java.io.IOException

import java.util.Arrays

import java.util.Iterator

import java.util.List

/**

* Created by zkn

*/

@Component("fileUploadInterceptor")

@ConfigurationProperties(prefix = "fileupload")

public class FileUploadInterceptor extends HandlerInterceptorAdapter {

private List<String>allowFileTypeList

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

throws Exception {

//文件上传的Servlet

if (request instanceof MultipartHttpServletRequest) {

//允许所有的文件类型

if (allowFileTypeList == null) {

return super.preHandle(request, response, handler)

}

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request

Iterator<String>it = multipartRequest.getFileNames()

if (it != null) {

while (it.hasNext()) {

String fileParameter = it.next()

List<MultipartFile>listFile = multipartRequest.getFiles(fileParameter)

if (!CollectionUtils.isEmpty(listFile)) {

MultipartFile multipartFile = null

String fileName = ""

for (int i = 0i <listFile.size()i++) {

multipartFile = listFile.get(i)

fileName = multipartFile.getOriginalFilename()

int flag = 0

if ((flag = fileName.lastIndexOf(".")) >0) {

fileName = fileName.substring(flag+1)

}

//不被允许的后缀名

if (!allowFileTypeList.contains(fileName)) {

this.outputStream(request, response)

return false

}

}

}

}

}

}

return super.preHandle(request, response, handler)

}

private void outputStream(HttpServletRequest request, HttpServletResponse response) {

response.setCharacterEncoding(request.getCharacterEncoding())

ServletOutputStream output = null

try {

output = response.getOutputStream()

output.write(("所传入的文件类型是不被允许的,允许的文件类型是:" + Arrays.toString(allowFileTypeList.toArray())).getBytes(request.getCharacterEncoding()))

} catch (IOException e) {

e.printStackTrace()

} finally {

if (output != null) {

try {

output.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

public void setAllowFileType(String allowFileType) {

//默认运行所有的类型

if (StringUtils.isEmpty(allowFileType)) {

allowFileTypeList = null

return

}

allowFileTypeList = Arrays.asList(allowFileType.split(","))

}

}

注册拦截器:

@Configuration

public class WebConfig extends WebMvcConfigurerAdapter {

@Autowired

private FileUploadInterceptor fileUploadInterceptor

//注册拦截器

@Override

public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(fileUploadInterceptor)

}

//配置加载静态资源

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/")

registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/")

super.addResourceHandlers(registry)

}

}

新增配置信息:

fileupload.allowFileType=txt,docs

这样我们的程序在运行的时候就会把不是txt或者docs文件类型的文件给过滤掉。

1.springboot访问静态资源的几种方式

(1)在src/main/resources/目录下创建

static文亏局培件夹

(2)在src/main/resources/目录下创建

resources文件夹

(3)在src/main/resources/目销唯录下创建

public文腊搏件夹

(4)在src/main/resources/目录下创建

META-INF/resources文件夹

---------------------

本文来自 吴锦涛1 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_34797335/article/details/80194137?utm_source=copy


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/bake/11967446.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存