多条告白如次剧本只需引入一次
零、本篇重心
引见SpringBoot对文献上传的机动摆设 。引见MultipartFile接口 。引见SpringBoot+Thymeleaf文献上传demo的调整 。引见对文献典型,文献名长度等确定本领 。一、SpringBoot对文献处置关系机动摆设
机动摆设是SpringBoot为咱们供给的便当之一,开拓者不妨在不作任何摆设的情景下,运用SpringBoot供给的默许树立,如处置文献须要的MultipartResolver 。
@Configuration(proxyBeanMethods = false)@ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class, MultipartConfigElement.class })@ConditionalOnProperty(prefix = "spring.servlet.multipart", name = "enabled", matchIfMissing = true)@ConditionalOnWebApplication(type = Type.SERVLET)@EnableConfigurationProperties(MultipartProperties.class)public class MultipartAutoConfiguration {private final MultipartProperties multipartProperties;public MultipartAutoConfiguration(MultipartProperties multipartProperties) {this.multipartProperties = multipartProperties;}@Bean@ConditionalOnMissingBean({ MultipartConfigElement.class, CommonsMultipartResolver.class })public MultipartConfigElement multipartConfigElement() {return this.multipartProperties.createMultipartConfig();}@Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)@ConditionalOnMissingBean(MultipartResolver.class)public StandardServletMultipartResolver multipartResolver() {StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily());return multipartResolver;}}Spring3.1之后扶助StandardServletMultipartResolver,且默许运用StandardServletMultipartResolver,它的便宜在乎:运用Servlet所供给的功效扶助,不须要依附任何其余的名目 。想要机动摆设奏效,须要摆设spring.servlet.multipart.enabled=true,固然这个摆设默许即是true 。关系的摆设树立在MultipartProperties中,个中字段即是对应的属性树立,典范字段有:enabled:能否打开文献上传机动摆设,默许打开 。location:上传文献的偶尔目次 。maxFileSize:更大文献巨细,以字节为单元,默许为1M 。maxRequestSize:所有乞求的更大含量,默许为10M 。fileSizeThreshold:文献巨细到达该阈值,将写入偶尔目次,默许为0,即一切文献城市径直写入磁盘偶尔文献中 。resolveLazily:能否惰性处置乞求,默许为false 。咱们也不妨自设置处置的详细,须要实行MultipartResolver接口 。二、处置上传文献MultipartFile接口
SpringBoot为咱们供给了MultipartFile宏大接口,让咱们不妨获得上传文献的精细消息,如原始文献名,实质典型之类,接口实质如次:
public interface MultipartFile extends InputStreamSource {String getName(); //获得参数名@NullableString getOriginalFilename();//原始的文献名@NullableString getContentType();//实质典型boolean isEmpty();long getSize(); //巨细byte[] getBytes() throws IOException;// 获得字节数组InputStream getInputStream() throws IOException;//以流办法举行读取default Resource getResource() {return new MultipartFileResource(this);}// 将上传的文献写入文献体例void transferTo(File var1) throws IOException, IllegalStateException;// 写入指定pathdefault void transferTo(Path dest) throws IOException, IllegalStateException {FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest));}}三、SpringBoot+Thymeleaf调整demo
1、编写遏制器
/** * 文献上传 * * @author Summerday */@Controllerpublic class FileUploadController {private static final String UPLOADED_FOLDER = System.getProperty("user.dir");@GetMapping("/")public String index() {return "file";}@PostMapping("/upload")public String singleFileUpload(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes) throws IOException {if (file.isEmpty()) {redirectAttributes.addFlashAttribute("msg", "文献为空,请采用你的文献上传");return "redirect:uploadStatus";}saveFile(file);redirectAttributes.addFlashAttribute("msg", "上传文献" + file.getOriginalFilename() + "胜利");redirectAttributes.addFlashAttribute("url", "/upload/" + file.getOriginalFilename());return "redirect:uploadStatus";}private void saveFile(MultipartFile file) throws IOException {Path path = Paths.get(UPLOADED_FOLDER + "/" + file.getOriginalFilename());file.transferTo(path);}@GetMapping("/uploadStatus")public String uploadStatus() {return "uploadStatus";}}2、编写页面file.html
<html xmlns:th="http://www.thymeleaf.org"><!--suppress ALL--><html lang="en"><head><meta charset="UTF-8"><title>文献上传界面</title></head><body><div><form method="POST" enctype="multipart/form-data" action="/upload"><table><tr><td><input type="file" name="file" /></td></tr><tr><td></td><td><input type="submit" value="https://www.juguize.com/b/上传" /></td></tr></table></form></div></body></html>3、编写页面uploadStatus.html
推荐阅读
- 西洋参含片上火吗 西洋参含片
- 玫瑰花茶上火吗
- 世界上最大的湖泊和最深的湖泊是什么 世界上最深的湖泊有哪些-这几个你都知道吗英语
- 世界上没有人类地球会变成什么样 如果没有现代人类,地球会有什么不同的现象
- 2015年评选出的100部最受欢迎美剧 史上最受欢迎的美剧
- 美国总统一个比一个差 美国历史上最差的一位总统
- 手机上出现hd什么意思,手机上出现hd什么意思怎么取消
- 珍珠马甲鱼吃什么饲料_珍珠马甲鱼属于上层鱼吗
- vivoy76s配置参数 vivoy76s值不值得入手
- 下里巴人现在寓意 下里巴人什么意思