2020.06-Study_update.4
week 6.22-6.28
- | Study-update |
---|---|
-Mon | 复习mvc |
-Tue | mvc文件上传 |
-Wes | Study-update |
-Thu | 端午快乐。 |
-Fri | spring配置druid |
-Sat | - |
-Sun | Study-update |
- | - |
6.23 Tuesday
文件上传
1.导包commons-fileupload
2.编写前端表单
注意
enctype=“multipart/form-data” type=“file”
<form action="${pageContext.request.contextPath}/book/upload2" method="post" enctype="multipart/form-data">文件:<input type="file" name="file"><input type="submit" name="提交">
</form>
3.配置bean:multipartResolver
id为multipartResolver 否则会报错
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"><property name="defaultEncoding" value="utf-8"/><property name="maxUploadSize" value="10485760"/><property name="maxInMemorySize" value="40960"/>
</bean>
4.Controller
普通IO保存文件
重点
//获取
String path = request.getSession().getServletContext().getRealPath("/upload");
//上传
@RequestMapping("/upload")
public String upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {//获取文件名String originalFilename = file.getOriginalFilename();//如果文件名为空if ("".equals(originalFilename)) {return "redirect:/book/allbook";}System.out.println("上传文件名为:" + originalFilename);//保存地址String path = request.getSession().getServletContext().getRealPath("/upload");File realPath = new File(path);if (!realPath.exists()) {realPath.mkdir();}System.out.println("文件保存位置为:" + realPath);//保存文件InputStream inputStream = file.getInputStream();OutputStream outputStream = new FileOutputStream(new File(realPath, originalFilename));int len = 0;byte[] buffer = new byte[1024];while ((len = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, len);outputStream.flush();}outputStream.close();inputStream.close();return "redirect:/book/allbook";
}
Transto方法
//transto方法
@RequestMapping("/upload2")
public String upload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {String path = request.getSession().getServletContext().getRealPath("/upload");File realPath = new File(path);if (!realPath.exists()) {realPath.mkdir();}//文件上传地址System.out.println("上传保存地址为:" + path);//通过CommonsMultipartFile的方法直接写文件file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));return "redirect:/book/allbook";
}
下载文件
@RequestMapping("/download")
public String download(HttpServletRequest request, HttpServletResponse response) throws Exception {//要下载的文件地址String path = request.getSession().getServletContext().getRealPath("/upload");String fileName = "新建文本文档.txt";//设置response响应头response.reset();response.setCharacterEncoding("utf-8");response.setContentType("multipart/form-data");//设置响应头response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));File file = new File(path, fileName);//2、 读取文件--输入流InputStream input = new FileInputStream(file);//3、 写出文件--输出流OutputStream out = response.getOutputStream();byte[] buff = new byte[1024];int index = 0;//4、执行 写出操作while ((index = input.read(buff)) != -1) {out.write(buff, 0, index);out.flush();}out.close();input.close();return null;
}
6.26 Friday
配置druid
1.导入德鲁伊的依赖
2.在application.yaml配置数据源
spring:datasource:username: rootpassword: root#?serverTimezone=UTC解决时区的报错url: jdbc:mysql://localhost:3306/webtest?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourcedruid:#Spring Boot 默认是不注入这些属性值的,需要自己绑定#druid 数据源专有配置initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4jfilters: stat,wall,log4jmaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
注意 这里的
专有配置得放在druid: 层级中,不然不识别。
3.导入log4j
4.配置专有数据源
package com.kuang.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;@Configuration
public class DruidConfig {/*将自定义的 Druid数据源添加到容器中,不再让 Spring Boot 自动创建绑定全局配置文件中的 druid 数据源属性到 com.alibaba.druid.pool.DruidDataSource从而让它们生效@ConfigurationProperties(prefix = "spring.datasource"):作用就是将 全局配置文件中前缀为 spring.datasource的属性值注入到 com.alibaba.druid.pool.DruidDataSource 的同名参数中*/@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DataSource druidDataSource() {return new DruidDataSource();}}
参考狂神文章学习
链接
2020.06-Study_update.4相关推荐
- 【算法训练】Leetcode 1295. 统计位数为偶数的数字(2020.06.09 )
1 题目 1295. 统计位数为偶数的数字 给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数. 示例 1: 输入:nums = [12,345,2,6,7896] 输出:2 解释: ...
- 树莓派最新最快更新源2020.06.06
更新源,简单地说,就是修改两个文件: 1.软件更新源(/etc/apt/sources.list) 2.系统更新源(/etc/apt/sources.d/raspi.list).我们对这两个文件进行修 ...
- 永恒之塔linux服务端,【永恒之塔单机5.8-6.5服务端】2020.06首发一键安装PC大型端游单机游戏客户端支持局域网联机玩[附视频搭建教程]...
[永恒之塔单机5.8-6.5服务端]2020.06首发一键安装PC大型端游单机游戏客户端支持局域网联机玩[附视频搭建教程] 01.支持全新机甲星技能.羽毛觉醒.GP点.觉醒水 02.优化怪物掉落数据, ...
- 2020.06.27 肉包去了喵星球
2020.06.27 17:34分 肉包永远的离开了我们 去了喵星球 他肯定回去当他的小王子去了 那么高冷帅气: 本来打算让肉包再陪伴一周 下去的时候鼻子出现了黄色的鼻涕 我就觉得不对劲了 ...
- beego orm Error 1045 [ORM]2020/06/12 22:17:09 register db Ping `default`, Error 1045: Access denied
[ORM]2020/06/12 22:17:09 register db Ping default, Error 1045: Access denied for user 'root'@'localh ...
- 2020.06新闻文章回顾
月初统一收租. 如果觉得每天的新闻&原创文章有点帮助,可以微微打赏,让德事君多一点坚持下去的动力! 2020.06新闻&文章 又来?!福特也开始做共享电动滑板车 柏林生鲜速递,10分钟 ...
- python爬虫获取天猫店经营者资质证书(更新到2020.06.13
python爬虫获取天猫店经营者资质证书(更新到2020.06.13 爬取需求 excel表中给定多个天猫的店铺链接,获取店铺的经营者资质证书,保存为本地图片 代码基于之前写的一个博客https:// ...
- 新XyPlayer 智能解析 X3.95正式版 (2020.06.12更新)
2020.06.12 更新 X3.9.5正式版 主要更新如下: 更新云播规则; 2.微信插件添加开关设置和防红开关; 注意:如果是更新升级,请在后台微信插件设置里重新启用. 2020.04.14 更新 ...
- 《惢客创业日记》2020.06.03-15(周三)为“创业日记”写序(一)
到今天为止,<惢客创业日记>80多万字的合集已经准备就绪了,等校对和排版工作完成后,应该就可以印刷成书了,从2018年9月1日开始,一直到2020年的6月1日,前前后后640天记录了惢客项 ...
- 【2020.06.01~2020.06.07】知识分享
当初建公众号定的OKR是文章数量能有高产土豆师傅的一半,看来是赶不上了. 写公众号目的是为了定期有的稳定的学习输出,索性就换成每周发一篇总结文,和大家一起分享在学习路上看到的风景. 0x01 业务安全 ...
最新文章
- qt combox 向上弹出_一睹芳容!人类首次拍到活的公羊角乌贼 手臂和触须向上飞速穿过水柱...
- 进阶:案例六: Context Menu(静态 与 动态)
- 深入浅出 Java CMS 学习笔记
- Ansible无敌详细入门教程
- linux进程map,LInux环境运行mapReduce程序
- 计算机网络之传输层:7、TCP拥塞控制
- /usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: No such file or directory 报错解决
- java设计模式之美_《设计模式之美》-笔记
- 服务器搭建成虚拟空间,服务器搭建虚拟空间
- linux scp命令 将数据从一台linux服务器复制到另一台linux服务器
- linux syn发包工具,发包工具 TRex stateless 使用笔记
- java自动发送qq消息
- 推荐系统中传统模型——LightGBM + LR融合
- 传感器实验——LCD显示SHT20
- LWC 61:741. Cherry Pickup
- S3C6410 SD Card一键烧写 WINCE 6.0
- 《数据结构》:中缀表达式合法性判断
- Linux学习_系统进程概念
- 电脑配音配音软件哪个好用?推荐3个好用软件
- mysql 以小时 分钟 为单位分组 30分钟 4小时