最近项目中用到了文件图片上传操作, 前端 使用<input type="file" class="ignore" accept="image/jpeg,image/png,image/gif"> (base64字节) 后台使用String字符串进行接收(base64字节) properties配置文件中进行文件上传大小配置 spring.http.multipart.max-file-size=20Mb //上传文件的大小限定;只有上传采用文件格式进行接收时起作用,针对上面的base64格式图片(后台是String进行接收)不起作用; spring.http.multipart.max-request-size=60Mb //上传请求数据的大小限定;限定请求的总数据大小 但是发现当上传文件大小大于1.5M时就会报错:
1 2 3 |
2018-06-08 11:03:19.006 ERROR 3784 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector] with root cause java.lang.IllegalStateException: The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector |
经过分析发现是springboot中的内置tomcat服务器限定了Httppost的最大size 通过在properties中添加如下配置,修改该内置服务器的对HttpPost数据的大小; 成功解决该问题; server.maxHttpHeaderSize=102400000 //设定HttpHeader请求头大小 server.maxHttpPostSize =102400000 //设定Httppost数据大小 from:https://www.cnblogs.com/AnXinliang/p/9154659.html
View Details最近做了一个项目,部署到客户的服务器上,运行状况也良好。就是客户的服务器经常会重启。然后客户也不是it人员,重启后重新启动服务,就得使用命令: jar -jar xxxx.jar 这对于不懂技术的人来说是个很复杂的事,于是决定打算修改修改,就有了下文。 一、编写bat文件启动jar 1、新建一个名为fmstart.bat,打开编辑,写入以下内容 cd E:/springboot java -jar fmstart-0.0.1-dev.jar exit 2、然后保存。ok,下次启动时,可以直接点击fmstart.bat启动了。至此愉快的玩耍了几天。 你以为这样就完了,过了几天,客户觉得点击bat启动也太麻烦了,我想一开机就启动。于是又得改。 二、windows启动菜单添加快捷方式启动 1、win+R打开运行窗口,输入shell:startup; 2、将fmstart.bat,新建一个快捷方式,然后将快捷方式拷贝到启动项文件中。 重启电脑,看到了熟悉的界面,启动成功。 又过了几天,客户看到我们的cmd界面也不爽了,说能否不让它出现,我容易误把它关掉。 于是又得改改改。。。。。 三、使用winsw将springboot打包的jar注册系统本地服务 【注】:本人只在Windows Server和win7测试,可以成功;win10会报错。等待作者更新新版本。 1、先从github上下载winsw,下载地址https://github.com/kohsuke/winsw 我下载的是2.1.2版本,小伙伴们可下载最新的版本。 2、下载WinSW.NET4.exe,这个是支持.netframework4.0的版本,如果没有.net4.0,请先安装.net4.0,下载后改名成你想启动的服务名字。比如:fmstart.exe 3、新建一个fmstart.xml文件,编写配置内容 <service> <id>fmstart</id> <name>fmstart</name> <description>This is fmstart service.</description> <!-- java环境变量 --> <env name="JAVA_HOME" value="%JAVA_HOME%"/> <executable>java</executable> <arguments>-jar "E:\springboot\fmstart-0.0.1-dev.jar"</arguments> <!-- 开机启动 --> <startmode>Automatic</startmode> <!-- 日志配置 --> <logpath>%BASE%\log</logpath> <logmode>rotate</logmode> </service> 4、保存fmstart.xml文件,将fmstart.xml和fmstart.exe放到要启动的jar的同级目录下。 例如:E:\springboot\ 5、打开cmd,进入到E:\springboot;输入fmstart.exe install安装服务。 安装后,输入fmstart.exe start启动服务; 相关命令如下: 除了install命令外,还有其它命令: uninstall:删除服务 start:启动服务 stop:停止服务 restart:重启服务 status:输出当前服务的状态 6、安装成功后,win+R打开运行,输入services.msc,查看是否有一个叫fmstart的服务。运行状态是否为正在运行。 如果安装成功后,服务状态为正在运行,即可测试你的接口来了。 到此结束了,客户每次启动服务器就会自己运行该服务,也不会有cmd小窗了。 ——————— 作者:mirfang 来源:CSDN 原文:https://blog.csdn.net/u012489412/article/details/81034375 版权声明:本文为博主原创文章,转载请附上博文链接!
View Details在项目构建的时候遇到了这样的问题:Failedto execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile(default-compile) on project taotao-manager-pojo: Compilation failure 检查了一下Installed JREs的设置,使用的环境变量为jre 解决方法: 将Installed JREs的设置修改为jdk即可.这里选择的其实是JAVA_HOME路径,jdk中包含jre 即可正常运行. ——————— 作者:陈晓婵 来源:CSDN 原文:https://blog.csdn.net/chenxiaochan/article/details/62036671 版权声明:本文为博主原创文章,转载请附上博文链接!
View Details1.右击项目,选择Run As – Maven clean 2.右击项目,选择Run As – Maven install 3.成功后 会在项目的target文件夹下生成jar包 4.将打包好的jar包 发布到服务器,运行java -jar jar包 5.或者使用命令nohup java -jar jar包,nohup命令可以后台启动jar,如果 直接运行 java -jar 则关闭终端,spring的进程也会关闭。 ——————— 作者:浅月流苏 来源:CSDN 原文:https://blog.csdn.net/wsf408908184/article/details/80760679 版权声明:本文为博主原创文章,转载请附上博文链接!
View Details简单说,Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:
1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 Thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
2.Thymeleaf 开箱即用的特性。它提供标准和 Spring 标准两种方言,可以直接套用模板实现 JSTL、 OGNL表达式效果,避免每天套模板、改 Jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
3.Thymeleaf 提供 Spring 标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
由于还使用elasticsearch,所以使用jedis没有使用lettuce 不然两个会冲突 1.pom引入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </exclusion> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> |
2.配置走一波,2.0版本之前的一些被移除了,我使用jedis就配置jedis了,使用lettuce的话一个样,换个名字就行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Redis_config # Redis数据库索引(默认为0) spring.redis.database=3 # Redis服务器地址 spring.redis.host=127.0.0.1 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接超时时间(毫秒) spring.redis.timeout=3600 # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # jedis超时 spring.redis.jedis.shutdown-timeout=100 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0 |
3.redis config走一波
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package cc.datebook.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set; /** * Redis缓存配置类 * @author wangH * */ @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Autowired private JedisConnectionFactory jedisConnectionFactory; /** * @description 自定义的缓存key的生成策略 * 若想使用这个key 只需要讲注解上keyGenerator的值设置为keyGenerator即可</br> * @return 自定义策略生成的key */ @Bean public KeyGenerator keyGenerator() { return new KeyGenerator(){ @Override public Object generate(Object target, Method method, Object... params) { StringBuffer sb = new StringBuffer(); sb.append(target.getClass().getName()); sb.append(method.getName()); for(Object obj:params){ sb.append(obj.toString()); } return sb.toString(); } }; } //缓存管理器 @Bean public RedisCacheManager cacheManager(JedisConnectionFactory jedisConnectionFactory) { return RedisCacheManager.create(jedisConnectionFactory); } /** * RedisTemplate配置 * * @param jedisConnectionFactory * @return */ @Bean public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory ) { //设置序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); //配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(jedisConnectionFactory); RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer);//key序列化 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//value序列化 redisTemplate.setHashKeySerializer(stringSerializer);//Hash key序列化 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);//Hash value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } } |
4.jedis config 走一波
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package cc.datebook.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration public class JedisRedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.max-wait}") private long maxWaitMillis; @Bean JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host); factory.setPort(port); factory.setTimeout(timeout); factory.setPassword(password); return factory; } @Bean public JedisPool redisPoolFactory() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); return jedisPool; } } |
5.然后直接用就行了
1 2 3 4 |
@Autowired private RedisTemplate redisTemplate; HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash(); |
from:https://blog.csdn.net/wangh92/article/details/79626969
View DetailsSpring Boot 对常用的数据库支持外,对 Nosql 数据库也进行了封装自动化。 Redis 介绍 Redis 是目前业界使用最广泛的内存数据存储。相比 Memcached,Redis 支持更丰富的数据结构,例如 hashes, lists, sets 等,同时支持数据持久化。除此之外,Redis 还提供一些类数据库的特性,比如事务,HA,主从库。可以说 Redis 兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景。本文介绍 Redis 在 Spring Boot 中两个典型的应用场景。 如何使用 1、引入依赖包
1 2 3 4 5 6 7 8 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> |
Spring Boot 提供了对 Redis 集成的组件包:spring-boot-starter-data-redis,spring-boot-starter-data-redis依赖于spring-data-redis 和 lettuce 。Spring Boot 1.0 默认使用的是 Jedis 客户端,2.0 替换成 Lettuce,但如果你从 Spring Boot 1.5.X 切换过来,几乎感受不大差异,这是因为 spring-boot-starter-data-redis 为我们隔离了其中的差异性。 Lettuce 是一个可伸缩线程安全的 Redis 客户端,多个线程可以共享同一个 RedisConnection,它利用优秀 netty NIO 框架来高效地管理多个连接。 2、添加配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=localhost # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) 默认 8 spring.redis.lettuce.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1 spring.redis.lettuce.pool.max-wait=-1 # 连接池中的最大空闲连接 默认 8 spring.redis.lettuce.pool.max-idle=8 # 连接池中的最小空闲连接 默认 0 spring.redis.lettuce.pool.min-idle=0 |
3、添加 cache 的配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport{ @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } } |
注意我们使用了注解:@EnableCaching来开启缓存。 3、好了,接下来就可以直接使用了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
@RunWith(SpringRunner.class) @SpringBootTest public class TestRedis { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate redisTemplate; @Test public void test() throws Exception { stringRedisTemplate.opsForValue().set("aaa", "111"); Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa")); } @Test public void testObj() throws Exception { User user=new User("aa@126.com", "aa", "aa123456", "aa","123"); ValueOperations<String, User> operations=redisTemplate.opsForValue(); operations.set("com.neox", user); operations.set("com.neo.f", user,1, TimeUnit.SECONDS); Thread.sleep(1000); //redisTemplate.delete("com.neo.f"); boolean exists=redisTemplate.hasKey("com.neo.f"); if(exists){ System.out.println("exists is true"); }else{ System.out.println("exists is false"); } // Assert.assertEquals("aa", operations.get("com.neo.f").getUserName()); } } |
以上都是手动使用的方式,如何在查找数据库的时候自动使用缓存呢,看下面; 4、自动根据方法生成缓存
1 2 3 4 5 6 7 8 9 10 11 |
@RestController public class UserController { @RequestMapping("/getUser") @Cacheable(value="user-key") public User getUser() { User user=new User("aa@126.com", "aa", "aa123456", "aa","123"); System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"); return user; } } |
其中 value 的值就是缓存到 Redis 中的 key 共享 Session 分布式系统中,Session 共享有很多的解决方案,其中托管到缓存中应该是最常用的方案之一, Spring Session 官方说明 Spring Session provides an API and implementations for […]
View Details上篇文章介绍了 Spring Boot 初级教程:Spring Boot(一):入门篇,方便大家快速入门、了解实践 Spring Boot 特性;本篇文章接着上篇内容继续为大家介绍 Spring Boot 的其它特性(有些未必是 Spring Boot 体系桟的功能,但是是 Spring 特别推荐的一些开源技术本文也会介绍),对了这里只是一个大概的介绍,特别详细的使用我们会在其它的文章中来展开说明。 Web 开发 Spring Boot Web 开发非常的简单,其中包括常用的 json 输出、filters、property、log 等 json 接口开发 在以前使用 Spring 开发项目,需要提供 json 接口时需要做哪些配置呢 添加 jackjson 等相关 jar 包 配置 Spring Controller 扫描 对接的方法添加 @ResponseBody 就这样我们会经常由于配置错误,导致406错误等等,Spring Boot 如何做呢,只需要类添加 @RestController 即可,默认类中的方法都会以 json 的格式返回
1 2 3 4 5 6 7 8 9 10 |
@RestController public class HelloController { @RequestMapping("/getUser") public User getUser() { User user=new User(); user.setUserName("小明"); user.setPassWord("xxxx"); return user; } } |
如果需要使用页面开发只要使用@Controller注解即可,下面会结合模板来说明 自定义 Filter 我们常常在项目中会使用 filters 用于记录调用日志、排除有 XSS 威胁的字符、执行权限验证等等。Spring Boot 自动添加了 OrderedCharacterEncodingFilter 和 HiddenHttpMethodFilter,并且我们可以自定义 Filter。 两个步骤: 实现 Filter 接口,实现 Filter 方法 添加@Configuration 注解,将自定义Filter加入过滤链 好吧,直接上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
@Configuration public class WebConfiguration { @Bean public RemoteIpFilter remoteIpFilter() { return new RemoteIpFilter(); } @Bean public FilterRegistrationBean testFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new MyFilter()); registration.addUrlPatterns("/*"); registration.addInitParameter("paramName", "paramValue"); registration.setName("MyFilter"); registration.setOrder(1); return registration; } public class MyFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) srequest; System.out.println("this is MyFilter,url :"+request.getRequestURI()); filterChain.doFilter(srequest, sresponse); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } } } |
自定义 Property 在 Web 开发的过程中,我经常需要自定义一些配置文件,如何使用呢 配置在 application.properties 中
1 2 |
com.neo.title=纯洁的微笑 com.neo.description=分享生活和技术 |
自定义配置类
1 2 3 4 5 6 7 8 9 10 |
@Component public class NeoProperties { @Value("${com.neo.title}") private String title; @Value("${com.neo.description}") private String description; //省略getter settet方法 } |
log配置 […]
View Details推荐博客 纯洁的微笑 程序猿DD liaokailin的专栏 Spring Boot 揭秘与实战 系列 catoop的专栏 简书Spring Boot专题 方志朋Spring Boot 专栏 Spring-boot集成 推荐网站 Spring boot 官网 Spring Boot参考指南-中文版 Gradle 中文参考指南 慕课网视频 spring-boot-tutorials 开源书籍-微服务:从设计到部署 开源代码 spring boot官方例子 spring-boot-examples SpringBoot-Learning favorites-web springboot-learning-example spring-boot-all from:http://www.ityouknow.com/springboot/2015/12/30/springboot-collect.html
View Details