索引: 開源Spring解決方案--lm.solution 參看代碼 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadController.java Files_Utils_DG.java 一、要點講解 1.引入文件上傳下載 ...
索引:
參看代碼 GitHub:
一、要點講解
1.引入文件上傳下載的類庫
commons-fileupload
commons-io
2.配置 MultipartResolver 組件(bean)
@Bean public MultipartResolver multipartResolver() : 該組件用來 解析 http multipart 類型的請求體
3.配置靜態文件的請求
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) :
該方法中註冊請求資源路徑,以便 href 鏈接中靜態資源的請求鏈接與下載。
registry.addResourceHandler("/files/**").addResourceLocations("classpath:/files/");
4.添加頁面快捷轉向,這樣就不用寫沒有邏輯僅做頁面轉向的 Controller 了
@Override public void addViewControllers(ViewControllerRegistry registry):
registry.addViewController("/upload").setViewName("fileupload/upload");
5.前臺表單中的設置
enctype="multipart/form-data" :在 form 表單中必須指定為 multipart
fileElementId:'file_AjaxFile' :在 ajax 中要指定 <input type="file"> 的 id
6.後臺中接收設置
@RequestParam("file_upload") MultipartFile multipartFile : 在方法參數中這樣指定後,就可以從請求體中讀取上傳的文件及文件信息了
7.其它詳細細節可具體參看代碼,及代碼中的解釋
... ...
二、詳細使用及代碼
1.pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <parent> 4 <artifactId>solution</artifactId> 5 <groupId>lm.solution</groupId> 6 <version>1.0-SNAPSHOT</version> 7 </parent> 8 <modelVersion>4.0.0</modelVersion> 9 <groupId>lm.solution</groupId> 10 <artifactId>web</artifactId> 11 <packaging>war</packaging> 12 <version>1.0-SNAPSHOT</version> 13 <name>web Maven Webapp</name> 14 <url>http://maven.apache.org</url> 15 <dependencies> 16 <!--Module--> 17 <dependency> 18 <groupId>lm.solution</groupId> 19 <artifactId>service</artifactId> 20 <version>1.0-SNAPSHOT</version> 21 </dependency> 22 <dependency> 23 <groupId>lm.solution</groupId> 24 <artifactId>common</artifactId> 25 <version>1.0-SNAPSHOT</version> 26 </dependency> 27 28 <!--Libary--> 29 <!--spring mvc--> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-webmvc</artifactId> 33 </dependency> 34 <!--cglib--> 35 <dependency> 36 <groupId>cglib</groupId> 37 <artifactId>cglib</artifactId> 38 </dependency> 39 <!-- mybatis核心包 --> 40 <dependency> 41 <groupId>org.mybatis</groupId> 42 <artifactId>mybatis</artifactId> 43 </dependency> 44 <!--mybatis spring 插件 --> 45 <dependency> 46 <groupId>org.mybatis</groupId> 47 <artifactId>mybatis-spring</artifactId> 48 </dependency> 49 <!-- Mysql資料庫驅動包 --> 50 <dependency> 51 <groupId>mysql</groupId> 52 <artifactId>mysql-connector-java</artifactId> 53 </dependency> 54 <!-- connection pool --> 55 <dependency> 56 <groupId>com.alibaba</groupId> 57 <artifactId>druid</artifactId> 58 <!--<scope>runtime</scope>--> 59 </dependency> 60 <!--servlet--> 61 <dependency> 62 <groupId>javax.servlet</groupId> 63 <artifactId>javax.servlet-api</artifactId> 64 <scope>provided</scope> 65 </dependency> 66 <dependency> 67 <groupId>javax.servlet.jsp</groupId> 68 <artifactId>jsp-api</artifactId> 69 <scope>provided</scope> 70 </dependency> 71 <dependency> 72 <groupId>javax.servlet</groupId> 73 <artifactId>jstl</artifactId> 74 </dependency> 75 <!-- 映入JSON lib --> 76 <dependency> 77 <groupId>net.sf.json-lib</groupId> 78 <artifactId>json-lib</artifactId> 79 <classifier>jdk15</classifier> 80 </dependency> 81 <!-- 用dom4j解析xml文件 --> 82 <dependency> 83 <groupId>dom4j</groupId> 84 <artifactId>dom4j</artifactId> 85 </dependency> 86 <!-- ehcache --> 87 <dependency> 88 <groupId>net.sf.ehcache</groupId> 89 <artifactId>ehcache-core</artifactId> 90 </dependency> 91 <dependency> 92 <groupId>net.sf.ehcache</groupId> 93 <artifactId>ehcache-web</artifactId> 94 </dependency> 95 <!-- 上傳組件包 --> 96 <dependency> 97 <groupId>commons-fileupload</groupId> 98 <artifactId>commons-fileupload</artifactId> 99 </dependency> 100 <dependency> 101 <groupId>commons-io</groupId> 102 <artifactId>commons-io</artifactId> 103 </dependency> 104 <!-- common others --> 105 <dependency> 106 <groupId>commons-codec</groupId> 107 <artifactId>commons-codec</artifactId> 108 </dependency> 109 <dependency> 110 <groupId>org.apache.commons</groupId> 111 <artifactId>commons-collections4</artifactId> 112 </dependency> 113 <dependency> 114 <groupId>org.apache.commons</groupId> 115 <artifactId>commons-lang3</artifactId> 116 </dependency> 117 <!-- commons-beanutils --> 118 <dependency> 119 <groupId>commons-beanutils</groupId> 120 <artifactId>commons-beanutils</artifactId> 121 <exclusions> 122 <exclusion> 123 <groupId>commons-logging</groupId> 124 <artifactId>commons-logging</artifactId> 125 </exclusion> 126 </exclusions> 127 </dependency> 128 <!-- freemarker --> 129 <dependency> 130 <groupId>org.freemarker</groupId> 131 <artifactId>freemarker</artifactId> 132 </dependency> 133 <!-- org.apache.httpcomponents --> 134 <dependency> 135 <groupId>org.apache.httpcomponents</groupId> 136 <artifactId>httpcore</artifactId> 137 </dependency> 138 <dependency> 139 <groupId>org.apache.httpcomponents</groupId> 140 <artifactId>httpclient</artifactId> 141 </dependency> 142 <!-- redis.clients/jedis --> 143 <dependency> 144 <groupId>redis.clients</groupId> 145 <artifactId>jedis</artifactId> 146 </dependency> 147 <!-- com.rabbitmq/amqp-client --> 148 <dependency> 149 <groupId>com.rabbitmq</groupId> 150 <artifactId>amqp-client</artifactId> 151 </dependency> 152 <!-- com.alibaba/fastjson --> 153 <dependency> 154 <groupId>com.alibaba</groupId> 155 <artifactId>fastjson</artifactId> 156 </dependency> 157 </dependencies> 158 <build> 159 <finalName>web</finalName> 160 161 <plugins> 162 <plugin> 163 <groupId>org.apache.tomcat.maven</groupId> 164 <artifactId>tomcat8-maven-plugin</artifactId> 165 </plugin> 166 </plugins> 167 </build> 168 </project>
2.WebConfig.java
1 package lm.solution.web.config.configs; 2 3 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 4 import lm.solution.common.web.messageconverter.CustomMessageConverter; 5 import lm.solution.web.config.beans.TimerInterceptor; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.ComponentScan; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.http.MediaType; 10 import org.springframework.http.converter.HttpMessageConverter; 11 import org.springframework.web.multipart.MultipartResolver; 12 import org.springframework.web.multipart.commons.CommonsMultipartResolver; 13 import org.springframework.web.servlet.config.annotation.*; 14 import org.springframework.web.servlet.view.InternalResourceViewResolver; 15 import org.springframework.web.servlet.view.JstlView; 16 17 import java.util.ArrayList; 18 import java.util.List; 19 20 @Configuration 21 /** 22 * @EnableWebMvc 註解會開啟一些預設配置,如:ViewResolver MessageConverter 等, 23 * 若無此註解,重寫 WebMvcConfigurerAdapter 方法無效 24 * */ 25 @EnableWebMvc 26 @ComponentScan(value = { 27 "lm.solution.web", 28 "lm.solution.service.mysql", 29 "lm.solution.service.webtest" 30 }) 31 /** 32 * 繼承 WebMvcConfigurerAdapter 類,重寫其方法可對 spring mvc 進行配置 33 * */ 34 public class WebConfig extends WebMvcConfigurerAdapter { 35 36 // 重寫 addViewControllers 簡化頁面快捷轉向,這樣就可以不用配置 Controller 了 37 @Override 38 public void addViewControllers(ViewControllerRegistry registry) { 39 40 registry.addViewController("/").setViewName("index"); 41 registry.addViewController("/error").setViewName("error/error"); 42 registry.addViewController("/excel").setViewName("excel/excel"); 43 // 文件上傳下載 44 registry.addViewController("/upload").setViewName("fileupload/upload"); 45 registry.addViewController("/ImageValidateCodeLogin").setViewName("login/imageValidateCodeLogin"); 46 registry.addViewController("/restfulapi").setViewName("restful/user"); 47 registry.addViewController("/jaxwsri").setViewName("jaxwsri/wsri"); 48 registry.addViewController("/redis").setViewName("redis/jedis"); 49 registry.addViewController("/mybatisPage").setViewName("db/mybatis"); 50 registry.addViewController("/messageconverter").setViewName("httpmessageconverter/customconverter"); 51 registry.addViewController("/sse").setViewName("serverpushmessage/sse"); 52 53 } 54 55 // 配置JSP視圖解析器 56 @Bean 57 public InternalResourceViewResolver viewResolver() { 58 InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 59 /** 60 * views 在 /resources/ 下 61 * */ 62 // 首碼 63 viewResolver.setPrefix("/WEB-INF/classes/views/"); 64 // 尾碼 65 viewResolver.setSuffix(".jsp"); 66 viewResolver.setViewClass(JstlView.class); 67 viewResolver.setContentType("text/html"); 68 // 可以在JSP頁面中通過${}訪問beans 69 viewResolver.setExposeContextBeansAsAttributes(true); 70 return viewResolver; 71 } 72 73 // 配置springMVC處理上傳文件的信息 74 @Bean 75 public MultipartResolver multipartResolver() { 76 CommonsMultipartResolver resolver = new CommonsMultipartResolver(); 77 resolver.setDefaultEncoding("UTF-8"); 78 resolver.setMaxUploadSize(10485760000L); 79 resolver.setMaxInMemorySize(40960); 80 return resolver; 81 } 82 83 // 配置靜態文件處理 84 @Override 85 public void addResourceHandlers(ResourceHandlerRegistry registry){ 86 87 /** 88 * addResourceHandler 指的是對外暴露的訪問路徑 89 * addResourceLocations 指的是文件放置的目錄 90 * */ 91 registry.addResourceHandler("/assets/**") 92 .addResourceLocations("classpath:/assets/"); 93 94 // href 鏈接方式 下載文件 95 registry.addResourceHandler("/files/**") 96 .addResourceLocations("classpath:/files/"); 97 98 /** 99 * 解決 No handler found for GET /favicon.ico 異常 100 * */ 101 registry.addResourceHandler("/favicon.ico") 102 .addResourceLocations("classpath:/favicon.ico"); 103 104 } 105 106 // 重寫 configurePathMatch ,改變路徑參數匹配 107 @Override 108 public void configurePathMatch(PathMatchConfigurer configurer) { 109 110 /** 111 * Spring mvc 預設 如果路徑參數後面帶點,如 “/mm/nn/xx.yy” 後面的yy值將被忽略 112 * 加入下麵的配置,就不會忽略“.”後面的參數了 113 * */ 114 configurer.setUseSuffixPatternMatch(false); 115 116 } 117 118 // 119 // // 負責讀取二進位格式的數據和寫出二進位格式的數據; 120 // @Bean 121 // public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { 122 // 123 // return new ByteArrayHttpMessageConverter(); 124 // 125 // } 126 // 127 //