Servlet3.0 multipart 文件上传技术
生活随笔
收集整理的這篇文章主要介紹了
Servlet3.0 multipart 文件上传技术
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Servlet3.0 javaConfig配置 傳統的servlet都是在web.xml中配置,從Servlet 3.0開始提供了ServletContainerInitializer接口,允許使用代碼去配置servlets、filters、listeners。 ?Spring為我們提供了一個該接口的實現類SpringServletContainerInitializer,查看源代碼可以知道該類通過@HandlesTypes()注解指定了onStartup()方法的第一個參數接收WebApplicationInitializer實現類的集合。所以如果我們要使用這種方式配置servlet,只需要實現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 注解? 相當于傳統配置文件中的Beans,該類中的方法可以通過?@Bean標注為 bean @ConfigurationpublicclassSpringConfig { ??? @Bean(name = "exampleBean") ??? public ExampleBean getExampleBean() { ??????? returnnew ExampleBean(); ??? } } @ComponentScan 注解 標明要掃描注解的包,相當于配置文件中的 context:component-scan。Spring會自動掃描注冊指定包中使用注解指定的Bean。 @ComponentScan(basePackages = {"com.example.service","com.example.dao"}) @PropertySource? 注解 可以引入 properties配置文件,通過注入Environment對象可以很方便的拿到配置文件中的內容。 @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; ??? } } 文件上傳功能簡介 基本概念 要使用基于Servlet 3.0的多路傳輸轉換功能: 1、在web.xml中為DispatcherServlet添加一個multipart-config元素 2、通過Servlet編程的方法使用MultipartConfigElement進行注冊 3、自己定制了自己的Servlet類,那你必須使用 @MultipartConfig對其進行注解。 注意:諸如最大文件大小或存儲位置等配置選項都必須在這個Servlet級別進行注冊,因為Servlet 3.0不允許在解析器MultipartResolver的層級配置這些信息。 ? ? 使用方法 在以前,處理文件上傳是一個很痛苦的事情,大都借助于開源的上傳組件,諸如commons fileupload等。現在好了,很方便,便捷到比那些組件都方便至極。 讓Servlet支持上傳,需要做兩件事情 需要添加MultipartConfig注解 從request對象中獲取Part文件對象 MultipartConfig注解
一些實踐建議: 若是上傳一個文件,僅僅需要設置maxFileSize屬性即可。 上傳多個文件,可能需要設置maxRequestSize屬性,設定一次上傳數據的最大量。 上傳過程中無論是單個文件超過maxFileSize值,或者上傳總的數據量大于maxRequestSize值都會拋出IllegalStateException異常; location屬性,既是保存路徑(在寫入的時候,可以忽略路徑設定),又是上傳過程中臨時文件的保存路徑,一旦執行Part.write方法之后,臨時文件將被自動清除。 但Servlet 3.0規范同時也說明,不提供獲取上傳文件名的方法,盡管我們可以通過part.getHeader("content-disposition")方法間接獲取得到。 如何讀取MultipartConfig注解屬性值,API沒有提供直接讀取的方法,只能手動獲取。 參考文檔: Servlet 3.0筆記之超方便的文件上傳支持 表單多文件上傳樣式美化 && 支持選中文件后刪除相關項
| 屬性名 | 類型 | 是否可選 | 描述 | ? |
| fileSizeThreshold | int | 是 | 當數據量大于該值時,內容將被寫入文件。 | ? |
| location | String | 是 | 存放生成的文件地址。 | ? |
| maxFileSize | long | 是 | 允許上傳的文件最大值。默認值為 -1,表示沒有限制。 | ? |
| maxRequestSize | long | 是 | 針對該 multipart/form-data 請求的最大數量,默認值為 -1,表示沒有限制。 | ? |
| ? | ? | ? | ? | ? |
轉載于:https://www.cnblogs.com/lemos/p/6534550.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Servlet3.0 multipart 文件上传技术的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Location和Content-Loc
- 下一篇: 20169207 2016-2017-2