您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“SpringMVC文件上傳的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
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.使用第三方j(luò)ar包 commons-upload
先導(dǎo)依賴 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中配置 <!--配置處理器分發(fā)器、文件上傳器--> <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> <!--臨時(shí)文件的目錄: 不存放臨時(shí)文件--> <location></location> <!-- 上傳文件最大2M --> <max-file-size>2097152</max-file-size> <!-- 上傳文件整個(gè)請(qǐng)求不超過(guò)4M --> <max-request-size>4194304</max-request-size> </multipart-config> </servlet>
2.使用第三方j(luò)ar包 commons-upload
先導(dǎo)依賴 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中配置 <!--配置處理器分發(fā)器、文件上傳器--> <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方法:對(duì)傳入文件使用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文件上傳的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。