SpringBoot实现文件上传下载的功能

来源:互联网 发布:gfxbench数据库 编辑:程序博客网 时间:2024/06/07 04:55
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 lang="en" 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", "attachment; filename="+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这个方法,然后将上传的文件信息存放到一个地方,根据个人的需求去做。
当我们下载文件时:

有时候我们可能需要限制上传文件的大小,可以这样设置上传文件的大小:
@Configurationpublic 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 = 0; i < 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(","));    }}

注册拦截器:

@Configurationpublic 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文件类型的文件给过滤掉。

原创粉丝点击