mirror of
https://gitee.com/gz-yami/mall4j.git
synced 2026-03-22 09:17:16 +08:00
新增文件上传到本地
This commit is contained in:
@@ -4420,95 +4420,6 @@ insert into `tz_prod_tag_reference`(`reference_id`,`shop_id`,`tag_id`,`prod_id`
|
||||
(340,NULL,2,18,1,'2019-06-22 18:28:31'),
|
||||
(341,NULL,3,18,1,'2019-06-22 18:28:31');
|
||||
|
||||
/*Table structure for table `tz_schedule_job` */
|
||||
|
||||
DROP TABLE IF EXISTS `tz_schedule_job`;
|
||||
|
||||
CREATE TABLE `tz_schedule_job` (
|
||||
`job_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '任务id',
|
||||
`bean_name` varchar(200) DEFAULT NULL COMMENT 'spring bean名称',
|
||||
`method_name` varchar(100) DEFAULT NULL COMMENT '方法名',
|
||||
`params` varchar(2000) DEFAULT NULL COMMENT '参数',
|
||||
`cron_expression` varchar(100) DEFAULT NULL COMMENT 'cron表达式',
|
||||
`status` tinyint(4) DEFAULT NULL COMMENT '任务状态 0:正常 1:暂停',
|
||||
`remark` varchar(255) DEFAULT NULL COMMENT '备注',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
PRIMARY KEY (`job_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COMMENT='定时任务';
|
||||
|
||||
/*Data for the table `tz_schedule_job` */
|
||||
|
||||
insert into `tz_schedule_job`(`job_id`,`bean_name`,`method_name`,`params`,`cron_expression`,`status`,`remark`,`create_time`) values
|
||||
(14,'orderTask','cancelOrder','','0 * * * * ?',0,'取消超时未支付订单','2019-06-24 14:51:51'),
|
||||
(16,'orderTask','confirmOrder','','0 0/5 * * * ?',0,'系统自动确认收货订单','2019-06-24 14:52:47');
|
||||
|
||||
/*Table structure for table `tz_schedule_job_log` */
|
||||
|
||||
DROP TABLE IF EXISTS `tz_schedule_job_log`;
|
||||
|
||||
CREATE TABLE `tz_schedule_job_log` (
|
||||
`log_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '任务日志id',
|
||||
`job_id` bigint(20) NOT NULL COMMENT '任务id',
|
||||
`bean_name` varchar(200) DEFAULT NULL COMMENT 'spring bean名称',
|
||||
`method_name` varchar(100) DEFAULT NULL COMMENT '方法名',
|
||||
`params` varchar(2000) DEFAULT NULL COMMENT '参数',
|
||||
`status` tinyint(4) NOT NULL COMMENT '任务状态 0:成功 1:失败',
|
||||
`error` varchar(2000) DEFAULT NULL COMMENT '失败信息',
|
||||
`times` int(11) NOT NULL COMMENT '耗时(单位:毫秒)',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
PRIMARY KEY (`log_id`),
|
||||
KEY `job_id` (`job_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=utf8 COMMENT='定时任务日志';
|
||||
|
||||
/*Data for the table `tz_schedule_job_log` */
|
||||
|
||||
insert into `tz_schedule_job_log`(`log_id`,`job_id`,`bean_name`,`method_name`,`params`,`status`,`error`,`times`,`create_time`) values
|
||||
(1,14,'orderTask','cancelOrder','',1,NULL,57,'2019-08-07 17:02:00'),
|
||||
(2,14,'orderTask','cancelOrder','',1,NULL,3,'2019-08-07 17:02:06'),
|
||||
(3,14,'orderTask','cancelOrder','',1,NULL,4,'2019-08-07 17:03:00'),
|
||||
(4,14,'orderTask','cancelOrder','',1,NULL,4,'2019-08-07 17:04:00'),
|
||||
(5,14,'orderTask','cancelOrder','',1,NULL,4,'2019-08-07 17:05:00'),
|
||||
(6,16,'orderTask','confirmOrder','',1,NULL,16,'2019-08-07 17:05:00'),
|
||||
(7,14,'orderTask','cancelOrder','',1,NULL,3,'2019-08-07 17:06:00'),
|
||||
(8,14,'orderTask','cancelOrder','',1,NULL,5,'2019-08-07 17:07:00'),
|
||||
(9,14,'orderTask','cancelOrder','',1,NULL,3,'2019-08-07 17:08:00'),
|
||||
(10,14,'orderTask','cancelOrder','',1,NULL,4,'2019-08-07 17:09:00'),
|
||||
(11,14,'orderTask','cancelOrder','',1,NULL,9,'2019-08-07 17:10:00'),
|
||||
(12,16,'orderTask','confirmOrder','',1,NULL,2,'2019-08-07 17:10:00'),
|
||||
(13,14,'orderTask','cancelOrder','',1,NULL,4,'2019-08-07 17:11:00'),
|
||||
(14,14,'orderTask','cancelOrder','',1,NULL,6,'2019-08-07 17:12:00'),
|
||||
(15,14,'orderTask','cancelOrder','',1,NULL,2,'2019-08-07 17:13:00'),
|
||||
(16,14,'orderTask','cancelOrder','',1,NULL,3,'2019-08-07 17:14:00'),
|
||||
(17,14,'orderTask','cancelOrder','',1,NULL,19,'2019-08-07 17:15:00'),
|
||||
(18,16,'orderTask','confirmOrder','',1,NULL,12,'2019-08-07 17:15:00'),
|
||||
(19,14,'orderTask','cancelOrder','',1,NULL,3,'2019-08-07 17:16:00'),
|
||||
(20,14,'orderTask','cancelOrder','',1,NULL,0,'2019-08-07 17:17:00'),
|
||||
(21,14,'orderTask','cancelOrder','',1,NULL,4,'2019-08-07 17:18:00'),
|
||||
(22,14,'orderTask','cancelOrder','',1,NULL,2,'2019-08-07 17:19:00'),
|
||||
(23,14,'orderTask','cancelOrder','',1,NULL,27,'2019-08-07 17:20:00'),
|
||||
(24,16,'orderTask','confirmOrder','',1,NULL,15,'2019-08-07 17:20:00'),
|
||||
(25,14,'orderTask','cancelOrder','',1,NULL,5,'2019-08-07 17:21:00'),
|
||||
(26,14,'orderTask','cancelOrder','',1,NULL,5,'2019-08-07 17:22:00'),
|
||||
(27,14,'orderTask','cancelOrder','',1,NULL,8,'2019-08-07 17:23:00'),
|
||||
(28,14,'orderTask','cancelOrder','',1,NULL,4,'2019-08-07 17:24:00'),
|
||||
(29,14,'orderTask','cancelOrder','',1,NULL,7,'2019-08-07 17:25:00'),
|
||||
(30,16,'orderTask','confirmOrder','',1,NULL,4,'2019-08-07 17:25:00'),
|
||||
(31,14,'orderTask','cancelOrder','',1,NULL,1,'2019-08-07 17:26:00'),
|
||||
(32,14,'orderTask','cancelOrder','',1,NULL,2,'2019-08-07 17:27:00'),
|
||||
(33,14,'orderTask','cancelOrder','',1,NULL,4,'2019-08-07 17:28:00'),
|
||||
(34,14,'orderTask','cancelOrder','',1,NULL,8,'2019-08-07 17:29:00'),
|
||||
(35,14,'orderTask','cancelOrder','',1,NULL,5,'2019-08-07 17:30:00'),
|
||||
(36,16,'orderTask','confirmOrder','',1,NULL,2,'2019-08-07 17:30:00'),
|
||||
(37,14,'orderTask','cancelOrder','',1,NULL,3,'2019-08-07 17:31:00'),
|
||||
(38,14,'orderTask','cancelOrder','',1,NULL,2,'2019-08-07 17:32:00'),
|
||||
(39,14,'orderTask','cancelOrder','',1,NULL,2,'2019-08-07 17:33:00'),
|
||||
(40,14,'orderTask','cancelOrder','',1,NULL,5,'2019-08-07 17:34:00'),
|
||||
(41,14,'orderTask','cancelOrder','',1,NULL,10,'2019-08-07 17:35:00'),
|
||||
(42,16,'orderTask','confirmOrder','',1,NULL,3,'2019-08-07 17:35:00'),
|
||||
(43,14,'orderTask','cancelOrder','',1,NULL,5,'2019-08-07 17:36:00'),
|
||||
(44,14,'orderTask','cancelOrder','',1,NULL,6,'2019-08-07 17:37:00'),
|
||||
(45,14,'orderTask','cancelOrder','',1,NULL,6,'2019-08-07 17:38:00');
|
||||
|
||||
/*Table structure for table `tz_shop_detail` */
|
||||
|
||||
DROP TABLE IF EXISTS `tz_shop_detail`;
|
||||
@@ -4856,15 +4767,6 @@ insert into `tz_sys_menu`(`menu_id`,`parent_id`,`name`,`url`,`perms`,`type`,`ic
|
||||
(2,1,'管理员列表','sys/user','',1,'admin',1),
|
||||
(3,1,'角色管理','sys/role','',1,'role',2),
|
||||
(4,1,'菜单管理','sys/menu','',1,'menu',3),
|
||||
(6,1,'定时任务','sys/schedule','',1,'job',5),
|
||||
(7,6,'查看',NULL,'sys:schedule:page,sys:schedule:info',2,NULL,0),
|
||||
(8,6,'新增',NULL,'sys:schedule:save',2,NULL,0),
|
||||
(9,6,'修改',NULL,'sys:schedule:update',2,NULL,0),
|
||||
(10,6,'删除',NULL,'sys:schedule:delete',2,NULL,0),
|
||||
(11,6,'暂停',NULL,'sys:schedule:pause',2,NULL,0),
|
||||
(12,6,'恢复',NULL,'sys:schedule:resume',2,NULL,0),
|
||||
(13,6,'立即执行',NULL,'sys:schedule:run',2,NULL,0),
|
||||
(14,6,'日志列表',NULL,'sys:schedule:log',2,NULL,0),
|
||||
(15,2,'查看',NULL,'sys:user:page,sys:user:info',2,NULL,0),
|
||||
(16,2,'新增','','sys:user:save,sys:role:list',2,'',1),
|
||||
(17,2,'修改','','sys:user:update,sys:role:list',2,'',2),
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
| 工具 | 版本 |
|
||||
|---------|-------|
|
||||
| jdk | 1.8+ |
|
||||
| jdk | 17 |
|
||||
| mysql | 5.7+ |
|
||||
| redis | 4.0+ |
|
||||
| nodejs | 14-16 |
|
||||
|
||||
@@ -219,3 +219,7 @@ shop.qiniu.bucket=
|
||||
|
||||
- 在`static/config/index.js` 修改`resourcesUrl` 统一七牛云存储开发环境目录
|
||||
- 在`static/config/index-prod.js` 修改`resourcesUrl` 统一七牛云存储生产环境目录
|
||||
|
||||
### 更新于2023.03.27
|
||||
- 在`shop.properties` 更新了本地上传的配置,vue中的`resourcesUrl`也配置对应的本地路径
|
||||
- 全局搜索`/mall4j/img`,替换成你想要的图片路径,若按本代码默认的则前端的资源路径为`http://ip: + admin服务的端口号/mall4j/img/`
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
(1)安装JDK
|
||||
|
||||
安装JDK,如果没有java-1.8.0-openjdk-devel就没有javac命令
|
||||
安装JDK,如果没有java-17-openjdk-devel就没有javac命令
|
||||
|
||||
```bash
|
||||
yum install java-1.8.0-openjdk java-1.8.0-openjdk-devel
|
||||
yum install java-17-openjdk java-17-openjdk-devel
|
||||
```
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
## 安装jdk
|
||||
|
||||
安装JDK,如果没有java-1.8.0-openjdk-devel就没有javac命令
|
||||
安装JDK,如果没有java-17-openjdk-devel就没有javac命令
|
||||
|
||||
```bash
|
||||
yum install java-1.8.0-openjdk java-1.8.0-openjdk-devel
|
||||
yum install java-17-openjdk java-17-openjdk-devel
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ package com.yami.shop.admin.controller;
|
||||
|
||||
import com.yami.shop.common.bean.Qiniu;
|
||||
import com.yami.shop.common.response.ServerResponseEntity;
|
||||
import com.yami.shop.common.util.ImgUploadUtil;
|
||||
import com.yami.shop.service.AttachFileService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -21,6 +22,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 文件上传 controller
|
||||
@@ -35,20 +37,28 @@ public class FileController {
|
||||
private AttachFileService attachFileService;
|
||||
@Autowired
|
||||
private Qiniu qiniu;
|
||||
@Autowired
|
||||
private ImgUploadUtil imgUploadUtil;
|
||||
|
||||
@PostMapping("/upload/element")
|
||||
public ServerResponseEntity<String> uploadElementFile(@RequestParam("file") MultipartFile file) throws IOException{
|
||||
if(file.isEmpty()){
|
||||
return ServerResponseEntity.success();
|
||||
}
|
||||
String fileName = attachFileService.uploadFile(file.getBytes(),file.getOriginalFilename());
|
||||
String fileName = attachFileService.uploadFile(file);
|
||||
return ServerResponseEntity.success(fileName);
|
||||
}
|
||||
|
||||
@PostMapping("/upload/tinymceEditor")
|
||||
public ServerResponseEntity<String> uploadTinymceEditorImages(@RequestParam("editorFile") MultipartFile editorFile) throws IOException{
|
||||
String fileName = attachFileService.uploadFile(editorFile.getBytes(),editorFile.getOriginalFilename());
|
||||
return ServerResponseEntity.success(qiniu.getResourcesUrl() + fileName);
|
||||
String fileName = attachFileService.uploadFile(editorFile);
|
||||
String data = "";
|
||||
if (Objects.equals(imgUploadUtil.getUploadType(), 1)) {
|
||||
data = imgUploadUtil.getUploadPath() + fileName;
|
||||
} else if (Objects.equals(imgUploadUtil.getUploadType(), 2)) {
|
||||
data = qiniu.getResourcesUrl() + fileName;
|
||||
}
|
||||
return ServerResponseEntity.success(data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
||||
*
|
||||
* https://www.mall4j.com/
|
||||
*
|
||||
* 未经允许,不可做商业用途!
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.yami.shop.common.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 本地存储配置信息
|
||||
* @author lgh
|
||||
*/
|
||||
@Data
|
||||
public class ImgUpload {
|
||||
|
||||
/**
|
||||
* 本地文件上传文件夹
|
||||
*/
|
||||
private String imagePath;
|
||||
|
||||
/**
|
||||
* 文件上传方式 1.本地文件上传 2.七牛云
|
||||
*/
|
||||
private Integer uploadType;
|
||||
|
||||
/**
|
||||
* 网站url
|
||||
*/
|
||||
private String resourceUrl;
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
package com.yami.shop.common.config;
|
||||
|
||||
import com.yami.shop.common.bean.ImgUpload;
|
||||
import com.yami.shop.common.enums.QiniuZone;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.yami.shop.common.config;
|
||||
|
||||
import com.yami.shop.common.util.ImgUploadUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author TRACK
|
||||
*/
|
||||
@Configuration
|
||||
public class ResourceConfigAdapter implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private ImgUploadUtil imgUploadUtil;
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/mall4j/img/**").addResourceLocations("file:" + imgUploadUtil.getUploadPath());
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
package com.yami.shop.common.config;
|
||||
|
||||
import com.yami.shop.common.bean.AliDaYu;
|
||||
import com.yami.shop.common.bean.ImgUpload;
|
||||
import com.yami.shop.common.bean.Qiniu;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@@ -43,4 +44,9 @@ public class ShopBasicConfig {
|
||||
*/
|
||||
private String tokenAesKey;
|
||||
|
||||
/**
|
||||
* 本地文件上传配置
|
||||
*/
|
||||
private ImgUpload imgUpload;
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ package com.yami.shop.common.config;
|
||||
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import com.yami.shop.common.bean.AliDaYu;
|
||||
import com.yami.shop.common.bean.ImgUpload;
|
||||
import com.yami.shop.common.bean.Qiniu;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -40,4 +41,9 @@ public class ShopBeanConfig {
|
||||
public AliDaYu aLiDaYu () {
|
||||
return shopBasicConfig.getALiDaYu();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ImgUpload imgUpload() {
|
||||
return shopBasicConfig.getImgUpload();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ public class ServerResponseEntity<T> implements Serializable {
|
||||
|
||||
public ServerResponseEntity() {
|
||||
// 版本号
|
||||
this.version = "mall4j.v230313";
|
||||
this.version = "mall4j.v230327";
|
||||
}
|
||||
|
||||
public static <T> ServerResponseEntity<T> success(T data) {
|
||||
|
||||
@@ -15,10 +15,12 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.yami.shop.common.bean.Qiniu;
|
||||
import com.yami.shop.common.util.ImgUploadUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author lanhai
|
||||
@@ -28,6 +30,8 @@ public class ImgJsonSerializer extends JsonSerializer<String> {
|
||||
|
||||
@Autowired
|
||||
private Qiniu qiniu;
|
||||
@Autowired
|
||||
private ImgUploadUtil imgUploadUtil;
|
||||
|
||||
@Override
|
||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
@@ -37,8 +41,14 @@ public class ImgJsonSerializer extends JsonSerializer<String> {
|
||||
}
|
||||
String[] imgs = value.split(StrUtil.COMMA);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String resourceUrl = "";
|
||||
if (Objects.equals(imgUploadUtil.getUploadType(), 2)) {
|
||||
resourceUrl = qiniu.getResourcesUrl();
|
||||
} else if (Objects.equals(imgUploadUtil.getUploadType(), 1)) {
|
||||
resourceUrl = imgUploadUtil.getResourceUrl();
|
||||
}
|
||||
for (String img : imgs) {
|
||||
sb.append(qiniu.getResourcesUrl()).append(img).append(StrUtil.COMMA);
|
||||
sb.append(resourceUrl).append(img).append(StrUtil.COMMA);
|
||||
}
|
||||
sb.deleteCharAt(sb.length()-1);
|
||||
gen.writeString(sb.toString());
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.yami.shop.common.util;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yami.shop.common.bean.ImgUpload;
|
||||
import com.yami.shop.common.exception.YamiShopBindException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 本地文件上传与删除
|
||||
* @author TRACK
|
||||
*/
|
||||
@Component
|
||||
public class ImgUploadUtil {
|
||||
|
||||
@Autowired
|
||||
private ImgUpload imgUpload;
|
||||
|
||||
public Integer getUploadType() {
|
||||
Integer uploadType = imgUpload.getUploadType();
|
||||
if (Objects.isNull(uploadType)) {
|
||||
throw new YamiShopBindException("请配置图片存储方式");
|
||||
}
|
||||
return uploadType;
|
||||
}
|
||||
|
||||
public String getUploadPath() {
|
||||
String imagePath = imgUpload.getImagePath();
|
||||
if (Objects.isNull(imagePath) || StrUtil.isBlank(imagePath)) {
|
||||
throw new YamiShopBindException("请配置图片存储路径");
|
||||
}
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
public String getResourceUrl() {
|
||||
String resourceUrl = imgUpload.getResourceUrl();
|
||||
if (Objects.isNull(resourceUrl) || StrUtil.isBlank(resourceUrl)) {
|
||||
throw new YamiShopBindException("请配置图片路径");
|
||||
}
|
||||
return resourceUrl;
|
||||
}
|
||||
|
||||
public String upload(MultipartFile img, String fileName) {
|
||||
String filePath = imgUpload.getImagePath();
|
||||
File file = new File(filePath + fileName);
|
||||
if (!file.exists()) {
|
||||
boolean result = file.mkdirs();
|
||||
if (!result) {
|
||||
throw new YamiShopBindException("创建目录:" + filePath + "失败");
|
||||
}
|
||||
}
|
||||
try {
|
||||
img.transferTo(file);
|
||||
} catch (IOException e) {
|
||||
throw new YamiShopBindException("图片上传失败");
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void delete(String fileName) {
|
||||
String filePath = imgUpload.getImagePath();
|
||||
File file = new File(filePath + fileName);
|
||||
file.deleteOnExit();
|
||||
}
|
||||
}
|
||||
@@ -10,3 +10,8 @@ shop.aLiDaYu.accessKeySecret=
|
||||
shop.aLiDaYu.signName=
|
||||
|
||||
shop.tokenAesKey=q5e6w6w9f4q497te
|
||||
|
||||
shop.imgUpload.imagePath=D:/Mall4j/IMG/
|
||||
# 1.\u672C\u5730\u6587\u4EF6\u4E0A\u4F20 2.\u4E03\u725B\u4E91
|
||||
shop.imgUpload.uploadType=1
|
||||
shop.imgUpload.resourceUrl=http://localhost:8085/mall4j/img/
|
||||
@@ -20,7 +20,8 @@ public class ResourceServerAdapter extends DefaultAuthConfigAdapter {
|
||||
"/swagger-ui.html",
|
||||
"/swagger-resources/**",
|
||||
"/captcha/**",
|
||||
"/adminLogin");
|
||||
"/adminLogin",
|
||||
"/mall4j/img/**");
|
||||
|
||||
@Override
|
||||
public List<String> excludePathPatterns() {
|
||||
|
||||
@@ -12,6 +12,7 @@ package com.yami.shop.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.yami.shop.bean.model.AttachFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -22,13 +23,12 @@ import java.io.IOException;
|
||||
public interface AttachFileService extends IService<AttachFile> {
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param bytes 字节
|
||||
* @param originalName 文件名称
|
||||
* @return 文件名称
|
||||
* @throws IOException 异常
|
||||
* 上传文件到本地
|
||||
* @param file
|
||||
* @throws IOException e
|
||||
* @return
|
||||
*/
|
||||
String uploadFile(byte[] bytes,String originalName) throws IOException;
|
||||
String uploadFile(MultipartFile file) throws IOException;
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
|
||||
@@ -23,14 +23,18 @@ import com.qiniu.storage.model.DefaultPutRet;
|
||||
import com.qiniu.util.Auth;
|
||||
import com.yami.shop.bean.model.AttachFile;
|
||||
import com.yami.shop.common.bean.Qiniu;
|
||||
import com.yami.shop.common.util.ImgUploadUtil;
|
||||
import com.yami.shop.common.util.Json;
|
||||
import com.yami.shop.dao.AttachFileMapper;
|
||||
import com.yami.shop.service.AttachFileService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author lanhai
|
||||
@@ -40,49 +44,52 @@ public class AttachFileServiceImpl extends ServiceImpl<AttachFileMapper, AttachF
|
||||
|
||||
@Autowired
|
||||
private AttachFileMapper attachFileMapper;
|
||||
|
||||
@Autowired
|
||||
private UploadManager uploadManager;
|
||||
|
||||
@Autowired
|
||||
private BucketManager bucketManager;
|
||||
@Autowired
|
||||
private Qiniu qiniu;
|
||||
|
||||
@Autowired
|
||||
private Auth auth;
|
||||
|
||||
@Autowired
|
||||
private ImgUploadUtil imgUploadUtil;
|
||||
public final static String NORM_MONTH_PATTERN = "yyyy/MM/";
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public String uploadFile(byte[] bytes,String originalName) throws QiniuException {
|
||||
String extName = FileUtil.extName(originalName);
|
||||
public String uploadFile(MultipartFile file) throws IOException {
|
||||
String extName = FileUtil.extName(file.getOriginalFilename());
|
||||
String fileName =DateUtil.format(new Date(), NORM_MONTH_PATTERN)+ IdUtil.simpleUUID() + "." + extName;
|
||||
|
||||
|
||||
AttachFile attachFile = new AttachFile();
|
||||
attachFile.setFilePath(fileName);
|
||||
attachFile.setFileSize(bytes.length);
|
||||
attachFile.setFileSize(file.getBytes().length);
|
||||
attachFile.setFileType(extName);
|
||||
attachFile.setUploadTime(new Date());
|
||||
attachFileMapper.insert(attachFile);
|
||||
|
||||
String upToken = auth.uploadToken(qiniu.getBucket(),fileName);
|
||||
Response response = uploadManager.put(bytes, fileName, upToken);
|
||||
Json.parseObject(response.bodyString(), DefaultPutRet.class);
|
||||
return fileName;
|
||||
if (Objects.equals(imgUploadUtil.getUploadType(), 1)) {
|
||||
// 本地文件上传
|
||||
attachFileMapper.insert(attachFile);
|
||||
return imgUploadUtil.upload(file, fileName);
|
||||
} else {
|
||||
// 七牛云文件上传
|
||||
String upToken = auth.uploadToken(qiniu.getBucket(),fileName);
|
||||
Response response = uploadManager.put(file.getBytes(), fileName, upToken);
|
||||
Json.parseObject(response.bodyString(), DefaultPutRet.class);
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFile(String fileName){
|
||||
attachFileMapper.delete(new LambdaQueryWrapper<AttachFile>().eq(AttachFile::getFilePath,fileName));
|
||||
try {
|
||||
bucketManager.delete(qiniu.getBucket(), fileName);
|
||||
if (Objects.equals(imgUploadUtil.getUploadType(), 1)) {
|
||||
imgUploadUtil.delete(fileName);
|
||||
} else if (Objects.equals(imgUploadUtil.getUploadType(), 2)) {
|
||||
bucketManager.delete(qiniu.getBucket(), fileName);
|
||||
}
|
||||
} catch (QiniuException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user