Skip to content

Commit e895508

Browse files
author
Administrator
committed
添加商品的时候上传图片
1 parent ebee28e commit e895508

File tree

10 files changed

+123
-2
lines changed

10 files changed

+123
-2
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ mall-server -> mall_zuul->mall_zuul_site -> mall_goods -> mall_user->mall_order-
3434

3535
服务监控访问:http://localhost:8088/
3636

37+
## 关于图片存储
38+
图片存储建议使用第三方比如又拍云
39+
40+
这边使用的是本地图片,本地图片需要一个服务器提供图片访问的能力
41+
42+
我这边使用是用tomcat作为图片服务器器 具体配置为 参数mall_goods模块中的配置文件
43+
44+
upload:
45+
path: /Users/dujinkai/Desktop/apache-tomcat-7.0.82/webapps/ROOT/ tomcat位置
46+
accessPath: http://localhost:8888/ 访问图片的路径
47+
3748
## 管理端截图
3849

3950
![image](https://raw.githubusercontent.com/djkdeveloper/mall/master/images/login.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.djk.utils;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.util.FileCopyUtils;
5+
6+
import java.io.File;
7+
8+
/**
9+
* Created by dujinkai on 2018/7/28.
10+
* 文件工具类
11+
*/
12+
public class FileUtils {
13+
private static FileUtils ourInstance = new FileUtils();
14+
15+
public static FileUtils getInstance() {
16+
return ourInstance;
17+
}
18+
19+
private FileUtils() {
20+
}
21+
22+
/**
23+
* 复制文件
24+
*
25+
* @param datas 图片数据
26+
* @param path 图片路径
27+
*/
28+
public void copyFile(byte[] datas, String path) {
29+
try {
30+
FileCopyUtils.copy(datas, new File(path));
31+
} catch (Exception e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
}

mall_goods/src/main/java/com/djk/feign/EsSpu.java

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public class EsSpu {
5353
*/
5454
private long tCateId;
5555

56+
/**
57+
* 图片地址
58+
*/
59+
private String pic;
60+
5661
/**
5762
* 创建时间
5863
*/

mall_goods/src/main/java/com/djk/spu/Spu.java

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public EsSpu convertToEsSpu() {
109109
esSpu.setSCateId(this.secondCateId);
110110
esSpu.setTCateId(this.thirdCateId);
111111
esSpu.setCreateTime(this.createTime);
112+
esSpu.setPic(this.pic);
112113
return esSpu;
113114
}
114115

mall_goods/src/main/java/com/djk/spu/SpuServiceImpl.java

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public PageHelper<Spu> querySpus(PageHelper<Spu> pageHelper, String name) {
6464
public int addSpu(Spu spu) {
6565
if (spuMapper.addSpu(spu) > 0) {
6666
esService.addSpuToEs(Arrays.asList(spu.convertToEsSpu()));
67+
return 1;
6768
}
6869
return 0;
6970
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.djk.upload;
2+
3+
import com.djk.utils.FileUtils;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
import org.springframework.web.multipart.MultipartFile;
9+
import org.springframework.web.multipart.MultipartHttpServletRequest;
10+
11+
import java.util.Objects;
12+
13+
/**
14+
* Created by dujinkai on 2018/7/28.
15+
* 上传控制器
16+
*/
17+
@RestController
18+
@RequestMapping("/upload")
19+
public class UploadController {
20+
21+
/**
22+
* 文件存放路径
23+
*/
24+
@Value("${upload.path}")
25+
private String path;
26+
27+
/**
28+
* 访问路径
29+
*/
30+
@Value("${upload.accessPath}")
31+
private String accessPath;
32+
33+
/**
34+
* 上传文件
35+
*
36+
* @param request 请求实体
37+
* @return 返回文件地址
38+
*/
39+
@PostMapping
40+
public String upload(MultipartHttpServletRequest request) throws Exception {
41+
MultipartFile multipartFile = request.getFile("file");
42+
if (Objects.isNull(multipartFile)) {
43+
return "";
44+
}
45+
// 文件名称
46+
String fileName = String.valueOf(System.currentTimeMillis()) + ".jpg";
47+
FileUtils.getInstance().copyFile(multipartFile.getBytes(), path + fileName);
48+
49+
return accessPath + fileName;
50+
}
51+
}

mall_goods/src/main/resources/application.yml

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ mybatis:
1818
configuration:
1919
map-underscore-to-camel-case: true
2020

21+
upload:
22+
path: /Users/dujinkai/Desktop/apache-tomcat-7.0.82/webapps/ROOT/
23+
accessPath: http://localhost:8888/
24+
2125

2226
server:
2327
port: 8083

mall_goods/src/main/resources/mapper/SpuMapper.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<update id="updateSpu" parameterType="com.djk.spu.Spu">
2121
UPDATE djk_spu SET name = #{name} , price = #{price},stock= #{stock}, brand_id=#{brandId} ,first_cate_id=#{firstCateId} ,
22-
second_cate_id = #{secondCateId},third_cate_id=#{thirdCateId},modify_time = now()
22+
second_cate_id = #{secondCateId},third_cate_id=#{thirdCateId},modify_time = now(),pic = #{pic}
2323
WHERE id =#{id}
2424
</update>
2525

@@ -47,6 +47,9 @@
4747
<if test="brandId != null">
4848
brand_id,
4949
</if>
50+
<if test="pic != null">
51+
pic,
52+
</if>
5053
del_flag,create_time
5154
</trim>
5255
<trim prefix="values (" suffix=")" suffixOverrides=",">
@@ -71,6 +74,9 @@
7174
<if test="brandId != null">
7275
#{brandId},
7376
</if>
77+
<if test="pic != null">
78+
#{pic},
79+
</if>
7480
'0',now()
7581
</trim>
7682
</insert>

mall_search/src/main/java/com/djk/es/EsSpu.java

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public class EsSpu {
6666
@Field(type = FieldType.Long)
6767
private long tCateId;
6868

69+
70+
/**
71+
* 图片地址
72+
*/
73+
@Field(type = FieldType.Keyword)
74+
private String pic;
75+
6976
/**
7077
* 创建时间
7178
*/

mall_zuul/src/main/resources/application.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ server:
1212

1313
ignore:
1414
urls: /login
15-
authUrls: /login,/manager/managerauthoritys,/authority/test
15+
authUrls: /login,/manager/managerauthoritys,/authority/test,/upload
1616

1717
jwt:
1818
jwtSecretKey: bGVjc2hvcF9qd3Rfc2VjcmV0X2tleQ==

0 commit comments

Comments
 (0)