Servlet3.0 multipart 文件上传技术
生活随笔
收集整理的這篇文章主要介紹了
Servlet3.0 multipart 文件上传技术
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Servlet3.0 javaConfig配置 傳統(tǒng)的servlet都是在web.xml中配置,從Servlet 3.0開始提供了ServletContainerInitializer接口,允許使用代碼去配置servlets、filters、listeners。 ?Spring為我們提供了一個(gè)該接口的實(shí)現(xiàn)類SpringServletContainerInitializer,查看源代碼可以知道該類通過(guò)@HandlesTypes()注解指定了onStartup()方法的第一個(gè)參數(shù)接收WebApplicationInitializer實(shí)現(xiàn)類的集合。所以如果我們要使用這種方式配置servlet,只需要實(shí)現(xiàn)WebApplicationInitializer?接口即可。 publicclassWebInitializerimplementsWebApplicationInitializer { ??? privatestaticfinal Logger logger = LoggerFactory.getLogger(WebInitializer.class); ??? @Override ??? publicvoidonStartup(javax.servlet.ServletContext servletContext) throws ServletException { ??????? logger.info("begin init web application."); ??????? //配置Spring ??????? AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext(); ??????? springContext.register(SpringConfig.class); ???????? ??????? //添加linstener ??????? servletContext.addListener(new ContextLoaderListener(springContext)); ??????? //添加servlet ??????? ServletRegistration.Dynamic dispatcher = servletContext.addServlet( ??????????????? "dispatcher", new DispatcherServlet(springContext)); ??????? dispatcher.setLoadOnStartup(1); ??????? dispatcher.addMapping("/"); ??????? //添加filter ??????? LoggerFilter loggerFilter = new LoggerFilter(); ??????? FilterRegistration.Dynamic logFilterRegistration=container.addFilter("requestResponseLogFilter", loggerFilter); ??????? logFilterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC), false, "/*"); ??????? logger.info("init web application success."); ??? } } Spring 配置 Spring的配置主要就是配置各種Bean,主要是要了解幾種注解的使用方法。 @Configuration 注解? 相當(dāng)于傳統(tǒng)配置文件中的Beans,該類中的方法可以通過(guò)?@Bean標(biāo)注為 bean @ConfigurationpublicclassSpringConfig { ??? @Bean(name = "exampleBean") ??? public ExampleBean getExampleBean() { ??????? returnnew ExampleBean(); ??? } } @ComponentScan 注解 標(biāo)明要掃描注解的包,相當(dāng)于配置文件中的 context:component-scan。Spring會(huì)自動(dòng)掃描注冊(cè)指定包中使用注解指定的Bean。 @ComponentScan(basePackages = {"com.example.service","com.example.dao"}) @PropertySource? 注解 可以引入 properties配置文件,通過(guò)注入Environment對(duì)象可以很方便的拿到配置文件中的內(nèi)容。 @Configuration@PropertySource("classpath:config.properties")@ComponentScan(basePackages = {"com.example.service","com.example.dao"})publicclassSpringConfig { ??? @Autowired ??? private Environment env; ??? @Bean(name = "mysqlDataSource") ??? public DataSource mysqlDataSource() { ??????? ProxoolDataSource dataSource = new ProxoolDataSource(); ??????? dataSource.setDriver(env.getProperty("ds.driver.classname")); ??????? dataSource.setDriverUrl(env.getProperty("ds.url")); ??????? dataSource.setUser(env.getProperty("ds.username")); ??????? dataSource.setPassword(env.getProperty("ds.password")); ??????? dataSource.setPrototypeCount(env.getProperty("proxool.prototype", Integer.class)); ??????? dataSource.setMinimumConnectionCount(env.getProperty("proxool.minimum", Integer.class)); ??????? dataSource.setMaximumConnectionCount(env.getProperty("proxool.maximum", Integer.class)); ??????? dataSource.setSimultaneousBuildThrottle(env.getProperty("proxool.simultaneous", Integer.class)); ??????? dataSource.setTestBeforeUse(true); ??????? dataSource.setHouseKeepingTestSql(env.getProperty("proxool.testSql")); ??????? return dataSource; ??? } } 文件上傳功能簡(jiǎn)介 基本概念 要使用基于Servlet 3.0的多路傳輸轉(zhuǎn)換功能: 1、在web.xml中為DispatcherServlet添加一個(gè)multipart-config元素 2、通過(guò)Servlet編程的方法使用MultipartConfigElement進(jìn)行注冊(cè) 3、自己定制了自己的Servlet類,那你必須使用 @MultipartConfig對(duì)其進(jìn)行注解。 注意:諸如最大文件大小或存儲(chǔ)位置等配置選項(xiàng)都必須在這個(gè)Servlet級(jí)別進(jìn)行注冊(cè),因?yàn)镾ervlet 3.0不允許在解析器MultipartResolver的層級(jí)配置這些信息。 ? ? 使用方法 在以前,處理文件上傳是一個(gè)很痛苦的事情,大都借助于開源的上傳組件,諸如commons fileupload等。現(xiàn)在好了,很方便,便捷到比那些組件都方便至極。 讓Servlet支持上傳,需要做兩件事情 需要添加MultipartConfig注解 從request對(duì)象中獲取Part文件對(duì)象 MultipartConfig注解
一些實(shí)踐建議: 若是上傳一個(gè)文件,僅僅需要設(shè)置maxFileSize屬性即可。 上傳多個(gè)文件,可能需要設(shè)置maxRequestSize屬性,設(shè)定一次上傳數(shù)據(jù)的最大量。 上傳過(guò)程中無(wú)論是單個(gè)文件超過(guò)maxFileSize值,或者上傳總的數(shù)據(jù)量大于maxRequestSize值都會(huì)拋出IllegalStateException異常; location屬性,既是保存路徑(在寫入的時(shí)候,可以忽略路徑設(shè)定),又是上傳過(guò)程中臨時(shí)文件的保存路徑,一旦執(zhí)行Part.write方法之后,臨時(shí)文件將被自動(dòng)清除。 但Servlet 3.0規(guī)范同時(shí)也說(shuō)明,不提供獲取上傳文件名的方法,盡管我們可以通過(guò)part.getHeader("content-disposition")方法間接獲取得到。 如何讀取MultipartConfig注解屬性值,API沒有提供直接讀取的方法,只能手動(dòng)獲取。 參考文檔: Servlet 3.0筆記之超方便的文件上傳支持 表單多文件上傳樣式美化 && 支持選中文件后刪除相關(guān)項(xiàng)
| 屬性名 | 類型 | 是否可選 | 描述 | ? |
| fileSizeThreshold | int | 是 | 當(dāng)數(shù)據(jù)量大于該值時(shí),內(nèi)容將被寫入文件。 | ? |
| location | String | 是 | 存放生成的文件地址。 | ? |
| maxFileSize | long | 是 | 允許上傳的文件最大值。默認(rèn)值為 -1,表示沒有限制。 | ? |
| maxRequestSize | long | 是 | 針對(duì)該 multipart/form-data 請(qǐng)求的最大數(shù)量,默認(rèn)值為 -1,表示沒有限制。 | ? |
| ? | ? | ? | ? | ? |
轉(zhuǎn)載于:https://www.cnblogs.com/lemos/p/6534550.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Servlet3.0 multipart 文件上传技术的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Location和Content-Loc
- 下一篇: 20169207 2016-2017-2