三阶段CGB2105-Day16

CGB2105-Day16

三阶段CGB2105-Day16 2021-08-10 11:53:08 三阶段CGB2105-Day16 3007 三阶段CGB2105-Day16三阶段CGB2105-Day16 收藏 12 分类专栏: 2105 版权 三阶段CGB2105-Day16 2105 专栏收录该内容 17 篇文章 26 订阅 订阅专栏

1. 商品管理业务

1.1 VUE 过滤器用法

/* 定义过滤器 */
/* Vue.filter("定义过滤器名称",function(参数){
   过滤器需要添加return
}) */
Vue.filter("priceFormat",function(price){
    //console.log(price)
    return (price / 100).toFixed(2)
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

三阶段CGB2105-Day16

1.2 商品新增操作

1.2.1 页面跳转

根据用户请求地址,实现页面跳转.

import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'
import Home from '../components/Home.vue'
import User from '../components/user/user.vue'
import Item from '../components/items/Item.vue'
import Welcome from '../components/Welcome.vue'
import ItemCat from '../components/items/ItemCat.vue'
import AddItem from '../components/items/addItem.vue'

//使用路由机制 通过children实现路由嵌套, redirect重定向
Vue.use(VueRouter)
const routes = [
{ path: ‘/’, redirect: ‘/login’},
{ path: ‘/login’, component: Login},
{ path: ‘/elementUI’, component: ElementUI},
//children组件的跳转 在home组件内部进行填充
{ path: ‘/home’, component: Home, redirect: ‘/welcome’, children:[
{ path: ‘/welcome’, component: Welcome},
{ path: ‘/user’, component: User},
{ path: ‘/item’, component: Item},
{ path: ‘/itemCat’, component: ItemCat},
{ path: ‘/item/addItem’, component: AddItem}
]}
]

  • 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

1.2.2 商品新增业务说明(一)

商品分为(item/itemDesc), 目前只完成商品基本信息的提交即可.
三阶段CGB2105-Day16

1.2.3 页面JS分析

 /* 添加商品按钮 */
      async addItemBtn(){
        //console.log(this.addItemForm)
    <span class="token comment">//1.完成表单校验</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">.</span>addItemFormRef<span class="token punctuation">.</span><span class="token function">validate</span><span class="token punctuation">(</span> valid <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{<!-- --></span>
      <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>valid<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"请输入商品必填项"</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>

    <span class="token comment">//2.完成商品参数的封装</span>
    <span class="token comment">//2.0 将商品价格扩大100倍</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>addItemForm<span class="token punctuation">.</span>price <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>addItemForm<span class="token punctuation">.</span>price <span class="token operator">*</span> <span class="token number">100</span>
    <span class="token comment">//2.1 将商品图片的数据转化为字符串</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>addItemForm<span class="token punctuation">.</span>images <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>addItemForm<span class="token punctuation">.</span>images<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span>

    <span class="token comment">//2.5 实现商品数据提交 商品(item基本信息/商品详情信息)</span>
    let submitAddItem <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span>
      item <span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>addItemForm<span class="token punctuation">,</span>
      itemDesc<span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>itemDesc
    <span class="token punctuation">}</span>


    <span class="token comment">//console.log(submitAddItem)</span>
    let <span class="token punctuation">{<!-- --></span>data<span class="token operator">:</span> result<span class="token punctuation">}</span> <span class="token operator">=</span> await <span class="token keyword">this</span><span class="token punctuation">.</span>$http<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">"/item/saveItem"</span><span class="token punctuation">,</span>submitAddItem<span class="token punctuation">)</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>result<span class="token punctuation">.</span>status <span class="token operator">!=</span><span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"商品添加失败"</span><span class="token punctuation">)</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$message<span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token string">"商品添加成功"</span><span class="token punctuation">)</span>

    <span class="token comment">//2.5添加完成之后,将数据重定向到商品展现页面</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">"/item"</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  • 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

1.2.4编辑ItemVO对象

/**
 * @author 刘昱江
 * 时间 2021/4/16
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class ItemVO {  //该对象封装商品所有的参数信息
    private Item item;
    private ItemDesc itemDesc;
    private ItemParam itemParam;
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.2.4 商品新增的业务接口

  • 请求路径: http://localhost:8091/item/saveItem
  • 请求类型: post
  • 前端传递参数分析
	{
		item: {
			images: "/2021/05/20/da0c1d4781c1499399f090da8b60f359.jpg,/2021/05/20/2ac1c34776a7465887eb019655354c3c.jpg"
			itemCatId: 560
			num: "100"
			price: 718800
			sellPoint: "【华为官方直供,至高12期免息0首付,原装正品】送华为原装无线充+运动蓝牙耳机+蓝牙音箱+三合一多功能数据线+钢化膜等!"
			title: "华为P40 Pro 5G手机【12期免息可选送豪礼】全网通智能手机"
		},
		itemDesc: {
				itemDesc: "<ul><li>品牌:&nbsp;<a href=https://list.jd.com/list.html".......      "
		}
	}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 请求参数: 使用ItemVO对象接收
参数名称 参数类型 参数说明 备注
item Item 商品基本信息对象封装 不能为null
itemDesc ItemDesc 商品详情信息 不能为null
  • ImageVO参数详解:
  • Item对象
参数名称 参数类型 参数说明 备注
title String 商品标题信息 不能为null
sellPoint String 商品卖点信息 不能为null
price Integer 商品价格信息 不能为null 需要将数据扩大100倍
num Integer 商品数量信息 不能为null
images String 商品图片地址信息 不能为null
itemCatId Integer 商品父级分类ID 不能为null
status Boolean 商品状态信息 不能为null
  • itemDesc 对象
  •  	为了降低商品提交代码的耦合性,将大字段信息详情,采用ItemDesc对象进行封装
    
       
       
    • 1
参数名称 参数类型 参数说明 备注
id Integer 商品Id信息 因为Item和ItemDesc是一对一关系 所以需要依赖Item对象的Id值
itemDesc String 商品详情信息 内部包含了大量的html语句
  • 返回值结果:
参数名称 参数说明 备注
status 状态信息 200表示服务器请求成功 201表示服务器异常
msg 服务器返回的提示信息 可以为null
data 服务器返回的业务数据 可以为null

1.3 商品详情说明

1.3.1 引入富文本编辑器

说明: 富文本用户操作的都是html代码片段

//1.导入JS
/* 导入富文本编辑器 */
import VueQuillEditor from 'vue-quill-editor'

/* 导入富文本编辑器对应的样式 */
import ‘quill/dist/quill.core.css’ // import styles
import ‘quill/dist/quill.snow.css’ // for snow theme
import ‘quill/dist/quill.bubble.css’ // for bubble theme

//2. 页面展现
<el-tab-pane label=“商品详情” name=“2”>
<!– 定义富文本编辑器–>
<quill-editor ref=“myQuillEditor” v-model=“itemDesc.itemDesc”>
</quill-editor>

<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 定义添加商品按钮<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token operator">&lt;</span>el<span class="token operator">-</span>button type<span class="token operator">=</span><span class="token string">"primary"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"addItemBtnClass"</span> <span class="token annotation punctuation">@click</span><span class="token operator">=</span><span class="token string">"addItemBtn"</span><span class="token operator">&gt;</span>添加商品<span class="token operator">&lt;</span><span class="token operator">/</span>el<span class="token operator">-</span>button<span class="token operator">&gt;</span>

</el-tab-pane>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

1.3.2 Item和ItemDesc关系

Item表: 主要封装了商品的基本信息.
ItemDesc表: 主要封装商品详情信息(大字段—html代码片段)
原因: 如果用户频繁的查询大字段 则影响效率. 所以将商品信息分为item和itemDesc
关联关系: item.id = itemDesc.id ID的值一致的.
三阶段CGB2105-Day16

1.2.4 编辑ItemController

  /**
     * 完成商品新增操作
     * 1.URL地址:  http://localhost:8091/item/saveItem
     * 2.参数: {item,itemDesc} 使用ItemVO进行接收
     * 3.请求类型: post    JSON
     * 4.返回值: SysResult对象
     */
    @PostMapping("/saveItem")
    public SysResult saveItem(@RequestBody ItemVO itemVO){
    itemService<span class="token punctuation">.</span><span class="token function">saveItem</span><span class="token punctuation">(</span>itemVO<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">SysResult</span><span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.2.5 编辑ItemServiceImpl

  /**
     * 完成商品入库操作
     * @param itemVO
     * 问题分析:
     *      1.item入库之后,才会有主键信息. 对象理论上的ID=null
     *      2.itemDesc入库时,必须获取与Item.id一样的数据.
     * 如何解决:
     *      设定主键自动回显功能!!!!!
     * 如何设计:
     *   开启主键自增  主键回显的配置  Mybatis原生操作
     *    <insert id="xxxx" useGeneratedKeys="true" keyColumn="主键字段" keyProperty="主键属性">
     *
     *    </insert>
     *  MybatisPlus:
     *        MP在完成入库操作时,自动的实现了数据的回显功能. 所以ID是有值的.
     *  知识: 哪种情况会有自动的回显功能!!!!!
     *  BUG:  由于测试数据可能会出现重复的现象. 需要提前删除多余的记录
     */
    @Override
    @Transactional  //事务控制
    public void saveItem(ItemVO itemVO) {
        //1.获取Item对象信息
        Item item = itemVO.getItem();
        item.setStatus(true);
        //2.商品入库操作  主键自增,入库之后才能看到主键. ID回显
        itemMapper.insert(item);
    <span class="token comment">//3.获取商品详情</span>
    <span class="token class-name">ItemDesc</span> itemDesc <span class="token operator">=</span> itemVO<span class="token punctuation">.</span><span class="token function">getItemDesc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//如何保证item和ItemDesc的ID一致?</span>
    itemDesc<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    itemDescMapper<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>itemDesc<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
  • 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

1.2.6 BUG说明

如果出现主键重复,会报入库异常.则需要提前删除多余测试数据
三阶段CGB2105-Day16

1.3 文件上传操作

1.3.1 官网API

  <!-- 图片上传操作
           file-list="fileList"  双向数据绑定 控制图片的数量数组结构[],
           :on-preview="handlePreview"  点击图片时候调用的函数
           :on-remove="handleRemove"    当用户点击删除按钮时,触发函数
           multiple 配置多选
           drag   是否启用拖拽
           action="图片提交的地址信息"
         -->
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          list-type="picture"
          multiple
          drag>
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
        </el-upload>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

1.3.2 文件上传的业务接口文档

  • 请求路径: http://localhost:8091/file/upload
  • 请求类型: post
  • 请求参数:
参数名称 参数说明 备注
file 文件上传的参数名称 file中携带的是二进制信息
  • 返回值结果:
参数名称 参数说明 备注
status 状态信息 200表示服务器请求成功 201表示服务器异常
msg 服务器返回的提示信息 可以为null
data 服务器返回的业务数据 返回ImageVO对象
  • ImageVO对象说明
参数名称 参数类型 参数说明 备注
virtualPath String 图片实际路径 不包含磁盘信息 例如: 2021/11/11/a.jpg 不需要写磁盘地址
urlPath String 图片url访问地址 http://image.jt.com/2021/11/11/a.jpg 需要指定域名地址
fileName String 文件上传后的文件名称 UUID.type

1.3.3 封装ImageVO对象

@Data
@Accessors(chain = true)
public class ImageVO {
    private String virtualPath; //虚拟路径
    private String urlPath;     //网络地址
    private String fileName;    //图片名称
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

1.3.4 文件上传入门案例

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {
<span class="token comment">/**
 * 业务: 文件上传入门案例
 * URL:  http://localhost:8091/file/upload
 * 参数:  file=[101001010111]
 * 返回值: SysResult对象(ImageVO)
 * 知识回顾: 字节流/字符流/缓存流  默认的语法复杂
 * 高级API:  SpringMVC 专门针对与流,开发了一个高级API
 * 文件上传步骤:
 *      1.获取文件上传名称
 *      2.准备文件上传的目录
 *      3.准备全文件的路径   目录/文件名称
 *      4.实现上传
 */</span>
<span class="token annotation punctuation">@PostMapping</span><span class="token punctuation">(</span><span class="token string">"/upload"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">SysResult</span> <span class="token function">upload</span><span class="token punctuation">(</span><span class="token class-name">MultipartFile</span> file<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{<!-- --></span>
    <span class="token comment">//1.动态获取文件名称</span>
    <span class="token class-name">String</span> fileName <span class="token operator">=</span> file<span class="token punctuation">.</span><span class="token function">getOriginalFilename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//2.准备文件目录  Linux系统不能识别\</span>
    <span class="token class-name">String</span> dirPath <span class="token operator">=</span> <span class="token string">"F:/images"</span><span class="token punctuation">;</span>
    <span class="token class-name">File</span> dirFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>dirPath<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>dirFile<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
        <span class="token comment">//应该创建一个新目录 创建多级目录</span>
        dirFile<span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//3.拼接文件路径</span>
    <span class="token class-name">String</span> filePath <span class="token operator">=</span> <span class="token string">"F:/images/"</span> <span class="token operator">+</span> fileName<span class="token punctuation">;</span>
    <span class="token comment">//4.实现文件上传</span>
    file<span class="token punctuation">.</span><span class="token function">transferTo</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>filePath<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"实现文件上传"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">SysResult</span><span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 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

1.3.5 正则表达式

正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。

语法:
三阶段CGB2105-Day16
匹配确定的次数:
例子: a{5} a出现5次
a{5,} a出现至少5次 >=5
a{5,8} a出现只能 5-8次
三阶段CGB2105-Day16
匹配任意字符
三阶段CGB2105-Day16
匹配字符区间范围
[xyz] 该字符只能取值 x/y/z中的一个 匹配单个字符
^ xyz 该字符除了xyz之外的其他字符.
[a-z] 该字符必须 a-z的区间中的一个
[0-9] 该字符必须 0-9的区间中的一个
三阶段CGB2105-Day16
分组结构:
(png|jpg|gif) 字符只能匹配png|jpg|gif中的一个 匹配的是字符串
三阶段CGB2105-Day16

1.3.6 编辑FileController

 /**
     * 需求分析: 文件上传完成之后,需要返回ImageVO对象
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {
    <span class="token class-name">ImageVO</span> imageVO <span class="token operator">=</span> fileService<span class="token punctuation">.</span><span class="token function">upload</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//不成功 应该返回null</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>imageVO <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
        <span class="token keyword">return</span> <span class="token class-name">SysResult</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token class-name">SysResult</span><span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span>imageVO<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.3.6 编辑FileServiceImpl(部分)

@Service
public class FileServiceImpl implements FileService{
<span class="token comment">/**
 * 1.校验文件上传的类型   jpg|png|gif
 * 2.应该校验文件是否为恶意程序.   木马.exe.jpg
 * 3.为了提高检索效率  应该分目录存储.  1.hash方式  xx/xx/xx/xx 分布不均
 *                                 2.日期格式   yyyy/MM/dd  目录不断增长
 * 4.防止文件重名        UUID.jpg
 * @param file
 * @return
 */</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token class-name">ImageVO</span> <span class="token function">upload</span><span class="token punctuation">(</span><span class="token class-name">MultipartFile</span> file<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    <span class="token comment">//1.图片类型的校验  正则表达式  aaa.jpg</span>
    <span class="token class-name">String</span> fileName <span class="token operator">=</span> file<span class="token punctuation">.</span><span class="token function">getOriginalFilename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//字符大小写 干扰正则的判断  将所有的文件转化为小写字母</span>
    fileName <span class="token operator">=</span> fileName<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//程序不满足正则, 则用户上传的图片有问题</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>fileName<span class="token punctuation">.</span><span class="token function">matches</span><span class="token punctuation">(</span><span class="token string">"^.+\\.(jpg|png|gif)$"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>

        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">//2. 校验文件是否为恶意程序  判断依据 属性宽度和高度  aa.exe.jpg</span>
    <span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span>
        <span class="token comment">//该对象是用来专门操作图片的API</span>
        <span class="token class-name">BufferedImage</span> bufferedImage <span class="token operator">=</span> <span class="token class-name">ImageIO</span><span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">getInputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> height <span class="token operator">=</span> bufferedImage<span class="token punctuation">.</span><span class="token function">getHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> width  <span class="token operator">=</span> bufferedImage<span class="token punctuation">.</span><span class="token function">getWidth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//如果有一项为0 则表示一定不是正经的图片</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>height <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> width <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
            <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//3.分目录存储文件  /yyyy/MM/dd</span>
        <span class="token comment">//3.1 准备文件根目录</span>
        <span class="token class-name">String</span> localDir <span class="token operator">=</span> <span class="token string">"F:/images"</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> dateDir <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleDateFormat</span><span class="token punctuation">(</span><span class="token string">"/yyyy/MM/dd/"</span><span class="token punctuation">)</span>
                         <span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//拼接文件目录  F:/images/2021/MM/dd/</span>
        <span class="token class-name">String</span> dirPath <span class="token operator">=</span> localDir <span class="token operator">+</span> dateDir<span class="token punctuation">;</span>
        <span class="token class-name">File</span> dirFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>dirPath<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//3.2 判断是否需要创建目录</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>dirFile<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//不存在目录时,应该创建目录</span>
            dirFile<span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//4.防止文件重名 UUID.后缀</span>
        <span class="token class-name">String</span> uuid <span class="token operator">=</span> UUID<span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"-"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//获取.的下标位置</span>
        <span class="token keyword">int</span> index <span class="token operator">=</span> fileName<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//截取文件类型</span>
        <span class="token class-name">String</span> fileType <span class="token operator">=</span>  fileName<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//拼接新文件路径</span>
        <span class="token class-name">String</span> realFileName <span class="token operator">=</span> uuid <span class="token operator">+</span> fileType<span class="token punctuation">;</span>

        <span class="token comment">//作业!!!!:  目录/文件名 实现文件上传  前端暂时不管</span>


    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>    <span class="token comment">//如果程序执行报错,则返回null</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 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
上一篇:Day16_Linux与nginx


下一篇:day16 Map Set