本篇内容介绍了“SpringMVC文件上传的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
java配置
1. 使用springMVC自带的文件上传器
在spring mvc配置类(public class SpringMVCConfig implements WebMvcConfigurer)中添加 @Bean(name = "multipartResolver") public MultipartResolver multipartResolver() { return new StandardServletMultipartResolver(); } 在web全局配置类(public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer)中配置 @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { registration.setMultipartConfig( new MultipartConfigElement(null,30*1024*1024,100*1024*1024,0)); }
2.使用第三方jar包 commons-upload
先导依赖 compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.4' 在spring mvc配置类(public class SpringMVCConfig implements WebMvcConfigurer)中添加 @Bean(name = "multipartResolver") public MultipartResolver multipartResolver() { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); multipartResolver.setMaxUploadSize(10*1024*1024); return multipartResolver; }
xml配置
1. 使用springMVC自带的文件上传器
spring-mvc.xml中配置 <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/> web.xml中配置 <!--配置处理器分发器、文件上传器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <multipart-config> <!--临时文件的目录: 不存放临时文件--> <location></location> <!-- 上传文件最大2M --> <max-file-size>2097152</max-file-size> <!-- 上传文件整个请求不超过4M --> <max-request-size>4194304</max-request-size> </multipart-config> </servlet>
2.使用第三方jar包 commons-upload
先导依赖 compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.4' 在spring-mvc.xml中配置 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="31457280"/> </bean> 在web.xml中配置 <!--配置处理器分发器、文件上传器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
前端:form表单
<form method="post" action="" enctype="multipart/form-data"> ...... </form>
controller方法:对传入文件使用MultipartFile类型
public String saveImage(Book book,@RequestParam("image") MultipartFile file){ if (file.getName().length()>15) return "redirect:/save.jsp"; var servlet = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); var request = Objects.requireNonNull(servlet).getRequest(); var realpath = request.getServletContext().getRealPath("/images/"); var realFile = new File(realpath); if (!realFile.exists())realFile.mkdirs(); try { file.transferTo(new File(realpath, Objects.requireNonNull(file.getOriginalFilename()))); } catch (IOException e) { e.printStackTrace(); } service.save(book); return ""; }
“SpringMVC文件上传的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。