本文共 7981 字,大约阅读时间需要 26 分钟。
然后在后台对应的处理方法中就可以直接获取到文件的输入流了。而对于SpringBoot来说,我们不需要配置文件上传的解析类了,因为SpringBoot已经帮我们注册好了。下面我们来看一下具体的开发。
这里我们用thymeleaf来作为页面的呈现,所以我们这里需要引入thymeleaf的相关依赖:
org.springframework.boot spring-boot-starter-thymeleaf
接着我们需要写一个文件的上传下载的页面,我简单的写了下面这个页面
Title
@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,会看到如下的页面(没有做排版处理):
@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 ListallowFileTypeList; @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 it = multipartRequest.getFileNames(); if (it != null) { while (it.hasNext()) { String fileParameter = it.next(); List 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文件类型的文件给过滤掉。