Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of uploading SpringMVC files

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the method of uploading SpringMVC files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Java configuration

1. Use the file uploader that comes with springMVC

Add @ Bean (name = "multipartResolver") public MultipartResolver multipartResolver () {return new StandardServletMultipartResolver () to the spring mvc configuration class (public class SpringMVCConfig implements WebMvcConfigurer);} configure @ Overrideprotected void customizeRegistration (ServletRegistration.Dynamic registration) {registration.setMultipartConfig (new MultipartConfigElement (null,30*1024*1024100*1024*1024,0));} in the web global configuration class (public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer)

two。 Use a third-party jar package commons-upload

The leader relies on compile group: 'commons-fileupload', name:' commons-fileupload', version: '1.4' to add @ Bean (name = "multipartResolver") public MultipartResolver multipartResolver () {CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver () to the spring mvc configuration class (public class SpringMVCConfig implements WebMvcConfigurer); multipartResolver.setMaxUploadSize (101024024); return multipartResolver;}

Xml configuration

1. Use the file uploader that comes with springMVC

Configure dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 2097152 4194304 in web.xml in spring-mvc.xml

two。 Use a third-party jar package commons-upload

The leader relies on compile group: 'commons-fileupload', name:' commons-fileupload', version: '1.4' configure in spring-mvc.xml configure dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 in web.xml

Front end: form form

.

Controller method: use the MultipartFile type for incoming files

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 "" } this is the end of the content of "SpringMVC File upload method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report