1.商品上架/下架操作
1.1 业务分析
当用户点击商品上架/下架的操作时,应该修改数据库中的状态信息status.
上架 status = 1, 下架 status = 2
1.2 页面URL分析
1.3 页面JS分析
1.4 实现RestFul调用
1.4.1 重构页面url地址
1.4.2 编辑ItemController
/** * 利用restFul方式实现状态修改. * 1./item/1 status=1 * 2./item/2 status=2 */ @RequestMapping("/{status}") public SysResult updateStatus(@PathVariable Integer status,Long[] ids){
itemService<span class="token punctuation">.</span><span class="token function">updateStatus</span><span class="token punctuation">(</span>ids<span class="token punctuation">,</span>status<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> SysResult<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
1.4.3 编辑ItemService
/**利用sql方式进行操作 * sql: update tb_item set status = #{status},updated=now() where id in (id......); * @param ids * @param status */ @Override public void updateStatus(Long[] ids, Integer status) {
itemMapper<span class="token punctuation">.</span><span class="token function">updateStatus</span><span class="token punctuation">(</span>ids<span class="token punctuation">,</span>status<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">/*//1.利用MP方式执行数据库操作 Item item = new Item(); item.setStatus(status); //定义修改操作的条件构造器 where id in (); UpdateWrapper<Item> updateWrapper = new UpdateWrapper<>(); List<Long> idList = Arrays.asList(ids); //数据转化为集合 updateWrapper.in("id",idList); //根据mp机制.实现批量的数据更新操作 itemMapper.update(item,updateWrapper);*/</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
1.4.4 编辑Mapper接口/xml映射文件
<!--更新商品的状态信息-->
<update id="updateStatus">
update tb_item set status = #{status},updated=now() where id in (
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)
</update>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.富文本编辑器
2.1富文本编辑器介绍
KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE、Firefox、Chrome、Safari、Opera等主流浏览器。
2.1富文本编辑器入门案例
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="/js/kindeditor-4.1.10/themes/default/default.css" type="text/css" rel="stylesheet">
<script type="text/javascript" charset="utf-8" src="/js/kindeditor-4.1.10/kindeditor-all-min.js"></script>
<script type="text/javascript" charset="utf-8" src="/js/kindeditor-4.1.10/lang/zh_CN.js"></script>
<script type="text/javascript" charset="utf-8" src="/js/jquery-easyui-1.4.1/jquery.min.js"></script>
<script type=“text/javascript”>
$(function(){
//在指定的为止初始化富文本.
KindEditor.ready(function(){
KindEditor.create("#editor")
})
})
</script>
</head>
<body>
<h1>富文本编辑器</h1>
<textarea style=“width:700px;height:350px” id=“editor”></textarea>
</body>
</html>
- 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
2.2商品表/商品分类表关系
说明:由于用户查询商品时,首先查询的是商品的主要信息.如果用户对某个商品感兴趣,才会查询商品详情信息.所以采用2张表的形式 展现商品/商品详情.
思考: 商品信息由商品/详情2部分构成.所有CRUD操作应该同时操作商品详情表.应该实现关联的操作.
2.3编辑ItemDesc POJO对象
package com.jt.pojo;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
/**
- 构建商品详情的POJO对象
*/
@TableName(“tb_item_desc”)
@Data
@Accessors(chain = true)
public class ItemDesc extends BasePojo{
<span class="token annotation punctuation">@TableId</span> <span class="token comment">//只标识主键即可</span>
<span class="token keyword">private</span> Long itemId<span class="token punctuation">;</span> <span class="token comment">//要求与商品表Id保持一致.</span>
<span class="token keyword">private</span> String itemDesc<span class="token punctuation">;</span> <span class="token comment">//商品详情信息</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
2.4编辑ItemDescMapper
package com.jt.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.ItemDesc;
public interface ItemDescMapper extends BaseMapper<ItemDesc> {
//对象与表进行绑定.如果自己不写sql则可以省略mapper映射文件
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.重构后台商品CRUD操作
3.1重构商品入库操作
3.1.1 页面参数提交
说明:在原有的商品新增的基础之上,完成商品详情新增操作.
3.1.2 编辑ItemController
动态接受参数信息 Item/ItemDesc.
3.1.3 编辑ItemService
/**
* 实现商品信息的入库操作
* 入库之前需要提前将数据补全. 刚新增的商品应该处于上架状态1
* @param item
* 注意事项:完成数据库更新操作时,需要注意数据库事务问题
*
* 完成商品表同时入库 难点: 如何保证商品ID和详情的ID一致????
* <insert id="xxxx" useGeneratedKeys="true" keyProperty="id" keyColumn="id"></insert>
*/
@Override
@Transactional
public void saveItem(Item item,ItemDesc itemDesc) {
//保证入库的时间一致
item.setStatus(1);
itemMapper.insert(item);
//分析问题: item表的主键是自增 数据库入库之后才会有主键生成.
//解决方案: 让数据库完成入库之后自动的实现主键的回显. 该操作由MP方式动态完成
itemDesc.setItemId(item.getId()); //必然有值
itemDescMapper.insert(itemDesc);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
3.2实现商品详情信息回显
3.2.1 页面url分析
说明:检查商品详情展现的url地址路径.
3.2.2 页面JS分析
3.2.3 编辑ItemController
说明:根据restFul风格实现商品分类查询.
/** * 业务:根据详情ID查询商品详情信息,之后再页面中回显. * url地址:http://localhost:8091/item/query/item/desc/1474391973 * 参数: 包含在url中,利用restFul方式动态获取 * 返回值: SysResult对象 */ @RequestMapping("/query/item/desc/{itemId}") public SysResult findItemDescById(@PathVariable Long itemId){
ItemDesc itemDesc <span class="token operator">=</span> itemService<span class="token punctuation">.</span><span class="token function">findItemDescById</span><span class="token punctuation">(</span>itemId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//将服务器数据返回页面</span> <span class="token keyword">return</span> SysResult<span class="token punctuation">.</span><span class="token function">success</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
3.2.4 编辑ItemService
//根据指定的ID查询商品详情信息 @Override public ItemDesc findItemDescById(Long itemId) {
<span class="token keyword">return</span> itemDescMapper<span class="token punctuation">.</span><span class="token function">selectById</span><span class="token punctuation">(</span>itemId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
3.2.5 页面效果展现
4 文件上传
4.1文件上传入门案例
4.1.1 编辑HTML页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>实现文件长传</h1>
<!--enctype="开启多媒体标签" -->
<form action="http://localhost:8091/file" method="post"
enctype="multipart/form-data">
<input name="fileImage" type="file" />
<input type="submit" value="提交"/>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
4.1.2 编辑FileController
package com.jt.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class FileController {
<span class="token comment">/**
* 完成文件上传的入门案例
* url地址: http://localhost:8091/file
* 请求参数: fileImage
* 返回值: 文件上传成功
*
* 利用SpringMVC中提供的工具API,实现文件上传的简化.
* 记住类型:MultipartFile
* 实现步骤:
* 1.接收资源文件
* 2.准备文件上传目录
* 3.准备文件上传的全路径 目录/文件名称
*/</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/file"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> String <span class="token function">file</span><span class="token punctuation">(</span>MultipartFile fileImage<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token comment">//2.文件文件上传的目录</span>
String fileDirPath <span class="token operator">=</span> <span class="token string">"D:/JT-SOFT/images"</span><span class="token punctuation">;</span>
File dirFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>fileDirPath<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>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 comment">//创建多级目录</span>
<span class="token punctuation">}</span>
<span class="token comment">//3.准备文件上传的全路径. 路径+文件名称</span>
String fileName <span class="token operator">=</span> fileImage<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">//文件名称.后缀 123.jgp</span>
File realFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>fileDirPath<span class="token operator">+</span><span class="token string">"/"</span><span class="token operator">+</span>fileName<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//将字节信息输出到文件中.</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
fileImage<span class="token punctuation">.</span><span class="token function">transferTo</span><span class="token punctuation">(</span>realFile<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//实现文件上传</span>
<span class="token keyword">return</span> <span class="token string">"文件上传成功!!!"</span><span class="token punctuation">;</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 string">"文件上传失败!!!"</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
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
4.2商品文件上传实现
4.2.1 富文本编辑器返回值说明
{“error”:0,“url”:“图片的保存路径”,“width”:图片的宽度,“height”:图片的高度}
属性1: error 如果在文件上传的过程中出现问题 则标识为1 ,如果没有错误 标识为0.
属性2: url 代表图片的虚拟访问地址. 磁盘地址
属性3: width/height 获取图片的宽高 可以省略.
4.2.2 封装文件上传的返回值VO
package com.jt.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class ImageVO implements Serializable {
<span class="token keyword">private</span> Integer error<span class="token punctuation">;</span> <span class="token comment">//确认是否有错误 0正常 1错误</span>
<span class="token keyword">private</span> String url<span class="token punctuation">;</span> <span class="token comment">//图片访问的虚拟地址.</span>
<span class="token keyword">private</span> Integer width<span class="token punctuation">;</span> <span class="token comment">//宽度</span>
<span class="token keyword">private</span> Integer height<span class="token punctuation">;</span> <span class="token comment">//高度</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> ImageVO <span class="token function">fail</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">new</span> <span class="token class-name">ImageVO</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> null<span class="token punctuation">,</span> null<span class="token punctuation">,</span> null<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> ImageVO <span class="token function">success</span><span class="token punctuation">(</span>String url<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ImageVO</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> url<span class="token punctuation">,</span> null<span class="token punctuation">,</span> null<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> ImageVO <span class="token function">success</span><span class="token punctuation">(</span>String url<span class="token punctuation">,</span>Integer width<span class="token punctuation">,</span>Integer height<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ImageVO</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> url<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<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
4.2.4 文件上传的参数说明
文件上传JS属性配置:
4.2.3 编辑FileController
说明: 文件上传时,需要注意富文本编辑器中传递的url参数.
/** * 实现图片上传操作. * url地址:http://localhost:8091/pic/upload?dir=image * 参数信息: uploadFile * 返回值: ImageVO对象 */ @RequestMapping("/pic/upload") public ImageVO upload(MultipartFile uploadFile){
<span class="token keyword">return</span> fileService<span class="token punctuation">.</span><span class="token function">upload</span><span class="token punctuation">(</span>uploadFile<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
4.2.3编辑FileService
package com.jt.service;
import com.jt.vo.ImageVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@Service
public class FileServiceImpl implements FileService{
<span class="token comment">//定义图片的类型集合.</span>
<span class="token keyword">private</span> <span class="token keyword">static</span> Set<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> imageTypeSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">private</span> String localDirPath <span class="token operator">=</span> <span class="token string">"D:/JT-SOFT/images"</span><span class="token punctuation">;</span> <span class="token comment">//定义本地磁盘目录</span>
<span class="token keyword">static</span> <span class="token punctuation">{</span>
imageTypeSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">".jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//字母小写类型</span>
imageTypeSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">".png"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
imageTypeSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">".gif"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//其他的省略.....</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 1.校验文件有效性 .jpg|.png|.gif.......
* 2.校验文件是否为恶意程序 (木马.exe).jpg
* 3.提高用户检索图片的效率 分目录存储.
* 4.为了防止重名图片的提交 自定义文件名称.
* 5.实现图片的物理上传 本地磁盘中.
* 6.准备一个访问图片的虚拟路径
* * @param uploadFile
* @return
*/</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> ImageVO <span class="token function">upload</span><span class="token punctuation">(</span>MultipartFile uploadFile<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//1.校验图片类型 1.利用正则表达式进行校验 2.利用集合进行校验 Set 数据是否存在即可.</span>
<span class="token comment">//1.1 获取图片名称 abc.jpg ABC.JPG</span>
String fileName <span class="token operator">=</span> uploadFile<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>
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 comment">//1.2 获取图片的类型</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>
String 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">//.jpg</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>imageTypeSet<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>fileType<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//如果类型不匹配</span>
<span class="token keyword">return</span> ImageVO<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 comment">//图片上传失败.</span>
<span class="token punctuation">}</span>
<span class="token comment">//2.如何判断文件是否为恶意程序? 文件是否有图片的特有属性!!!!</span>
<span class="token comment">//2.1将上传的文件类型利用图片的API进行转化 如果转化不成功则一定不是图片.</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
BufferedImage bufferedImage <span class="token operator">=</span> ImageIO<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>uploadFile<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 comment">//2.2校验是否有图片的特有属性 高度/宽度</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 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 comment">//2.3校验宽度和高度是否有值.</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>width <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> height <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> ImageVO<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 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> ImageVO<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 comment">//返回失败即可</span>
<span class="token punctuation">}</span>
<span class="token comment">//3.实现分目录存储</span>
<span class="token comment">// 方案1: 利用hash之后每隔2-3位截取之后拼接</span>
<span class="token comment">// 方案2: 以时间为单位进行分隔 /yyyy/MM/dd/</span>
<span class="token comment">//3.1 利用工具API将时间转化为指定的格式</span>
String datePath <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">//3.2 动态生成文件目录 2部分=根目录+时间目录</span>
String localDir <span class="token operator">=</span> localDirPath <span class="token operator">+</span> datePath<span class="token punctuation">;</span>
<span class="token comment">//3.3判断目录是否存在, 如果不存在则新建目录</span>
File dirFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>localDir<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>
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 comment">//如果不存在,则新建目录</span>
<span class="token punctuation">}</span>
<span class="token comment">//4. 防止文件重名,需要自定义文件名称 UUID</span>
<span class="token comment">//4.1生成uuid</span>
String 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">//4.2动态生成文件名称 uuid +.jpg</span>
String uuidFileName <span class="token operator">=</span> uuid <span class="token operator">+</span> fileType<span class="token punctuation">;</span>
<span class="token comment">//5.实现文件上传 准备文件全路径 目录+文件名称</span>
String realFilePath <span class="token operator">=</span> localDir <span class="token operator">+</span> uuidFileName<span class="token punctuation">;</span>
<span class="token comment">//5.1封装文件真实对象</span>
File imageFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>realFilePath<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//5.2实现文件上传</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
uploadFile<span class="token punctuation">.</span><span class="token function">transferTo</span><span class="token punctuation">(</span>imageFile<span class="token punctuation">)</span><span class="token punctuation">;</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> ImageVO<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 comment">//告知文件上传失败</span>
<span class="token punctuation">}</span>
<span class="token comment">//6.暂时使用京东图片代替.</span>
<span class="token comment">// 检查文件上传业务调用是否正确</span>
String url <span class="token operator">=</span> <span class="token string">"https://img14.360buyimg.com/n1/s546x546_jfs/t1/122216/5/8769/316276/5f2a160eE30a3fbfd/f742fb30fdea6239.jpg"</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> ImageVO<span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span>url<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
- 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
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
作业
1.仿照京东商品路径 以 http://image.jt.com/ 路径拼接 形成虚拟记录. 思考路径拼接注意事项
2.能否利用动态赋值的方式 修改属性值.
3.了解什么是方向代理/正向代理/nginx
</div><div data-report-view="{"mod":"1585297308_001","dest":"https://blog.csdn.net/qq_16804847/article/details/107806742","extend1":"pc","ab":"new"}"><div></div></div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-ff98e99283.css" rel="stylesheet">
</div>