张延森 4 vuotta sitten
vanhempi
commit
2e83626e14

+ 39
- 12
pom.xml Näytä tiedosto

24
 			<artifactId>spring-boot-starter-web</artifactId>
24
 			<artifactId>spring-boot-starter-web</artifactId>
25
 		</dependency>
25
 		</dependency>
26
 
26
 
27
+		<dependency>
28
+			<groupId>org.springframework.boot</groupId>
29
+			<artifactId>spring-boot-starter-test</artifactId>
30
+			<scope>test</scope>
31
+			<exclusions>
32
+				<exclusion>
33
+					<groupId>org.junit.vintage</groupId>
34
+					<artifactId>junit-vintage-engine</artifactId>
35
+				</exclusion>
36
+			</exclusions>
37
+		</dependency>
38
+
39
+		<!--mysql start-->
27
 		<dependency>
40
 		<dependency>
28
 			<groupId>mysql</groupId>
41
 			<groupId>mysql</groupId>
29
 			<artifactId>mysql-connector-java</artifactId>
42
 			<artifactId>mysql-connector-java</artifactId>
30
 			<scope>runtime</scope>
43
 			<scope>runtime</scope>
31
 		</dependency>
44
 		</dependency>
45
+		<!--mysql end-->
32
 
46
 
47
+		<!--mybatis-plus start-->
33
 		<dependency>
48
 		<dependency>
34
 			<groupId>com.baomidou</groupId>
49
 			<groupId>com.baomidou</groupId>
35
 			<artifactId>mybatis-plus-boot-starter</artifactId>
50
 			<artifactId>mybatis-plus-boot-starter</artifactId>
36
 			<version>3.1.1</version>
51
 			<version>3.1.1</version>
37
 		</dependency>
52
 		</dependency>
53
+		<!--mybatis-plus end-->
54
+
55
+		<!--fastjson start-->
38
 		<dependency>
56
 		<dependency>
39
 			<groupId>com.alibaba</groupId>
57
 			<groupId>com.alibaba</groupId>
40
 			<artifactId>fastjson</artifactId>
58
 			<artifactId>fastjson</artifactId>
41
 			<version>1.2.56</version>
59
 			<version>1.2.56</version>
42
 		</dependency>
60
 		</dependency>
61
+		<!--fastjson end-->
43
 
62
 
63
+		<!--jwt start-->
44
 		<dependency>
64
 		<dependency>
45
 			<groupId>io.jsonwebtoken</groupId>
65
 			<groupId>io.jsonwebtoken</groupId>
46
 			<artifactId>jjwt-api</artifactId>
66
 			<artifactId>jjwt-api</artifactId>
58
 			<version>0.11.2</version>
78
 			<version>0.11.2</version>
59
 			<scope>runtime</scope>
79
 			<scope>runtime</scope>
60
 		</dependency>
80
 		</dependency>
81
+		<!--jwt end-->
61
 
82
 
83
+		<!--oss start-->
62
 		<dependency>
84
 		<dependency>
63
 			<groupId>com.aliyun.oss</groupId>
85
 			<groupId>com.aliyun.oss</groupId>
64
 			<artifactId>aliyun-sdk-oss</artifactId>
86
 			<artifactId>aliyun-sdk-oss</artifactId>
65
 			<version>2.8.3</version>
87
 			<version>2.8.3</version>
66
 		</dependency>
88
 		</dependency>
89
+		<!--oss end-->
90
+
91
+		<!--excel start-->
92
+		<dependency>
93
+			<groupId>com.alibaba</groupId>
94
+			<artifactId>easyexcel</artifactId>
95
+			<version>2.0.4</version>
96
+		</dependency>
97
+		<!--excel end-->
98
+
99
+		<!--weixin-miniapp start-->
67
 		<dependency>
100
 		<dependency>
68
 			<groupId>com.github.binarywang</groupId>
101
 			<groupId>com.github.binarywang</groupId>
69
 			<artifactId>weixin-java-miniapp</artifactId>
102
 			<artifactId>weixin-java-miniapp</artifactId>
70
 			<version>3.8.0</version>
103
 			<version>3.8.0</version>
71
 		</dependency>
104
 		</dependency>
105
+		<!--weixin-miniapp start-->
106
+
107
+		<!--lombok start-->
72
 		<dependency>
108
 		<dependency>
73
 			<groupId>org.projectlombok</groupId>
109
 			<groupId>org.projectlombok</groupId>
74
 			<artifactId>lombok</artifactId>
110
 			<artifactId>lombok</artifactId>
75
 			<optional>true</optional>
111
 			<optional>true</optional>
76
 		</dependency>
112
 		</dependency>
113
+		<!--lombok end-->
77
 
114
 
78
-		<dependency>
79
-			<groupId>org.springframework.boot</groupId>
80
-			<artifactId>spring-boot-starter-test</artifactId>
81
-			<scope>test</scope>
82
-			<exclusions>
83
-				<exclusion>
84
-					<groupId>org.junit.vintage</groupId>
85
-					<artifactId>junit-vintage-engine</artifactId>
86
-				</exclusion>
87
-			</exclusions>
88
-		</dependency>
89
-
115
+		<!--swagger start-->
90
 		<dependency>
116
 		<dependency>
91
 			<groupId>io.springfox</groupId>
117
 			<groupId>io.springfox</groupId>
92
 			<artifactId>springfox-boot-starter</artifactId>
118
 			<artifactId>springfox-boot-starter</artifactId>
93
 			<version>3.0.0</version>
119
 			<version>3.0.0</version>
94
 		</dependency>
120
 		</dependency>
121
+		<!--swagger end-->
95
 	</dependencies>
122
 	</dependencies>
96
 
123
 
97
 	<profiles>
124
 	<profiles>

+ 26
- 0
src/main/java/com/yunzhi/niucai/common/ExcelUtils.java Näytä tiedosto

1
+package com.yunzhi.niucai.common;
2
+
3
+import com.alibaba.excel.EasyExcel;
4
+
5
+import javax.servlet.http.HttpServletResponse;
6
+import java.io.IOException;
7
+import java.util.List;
8
+
9
+public class ExcelUtils {
10
+
11
+    /**
12
+     * 发送 excel 到客户端
13
+     * 暂时只支持单 sheet 页
14
+     * @param response
15
+     * @param data
16
+     * @param fileName
17
+     * @throws IOException
18
+     */
19
+    public static void flush(HttpServletResponse response, Class dataClass, List data, String fileName) throws IOException {
20
+        response.setContentType("application/vnd.ms-excel");
21
+        response.setCharacterEncoding("utf-8");
22
+        response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName)+".xlsx");
23
+
24
+        EasyExcel.write(response.getOutputStream(), dataClass).sheet("sheet1").doWrite(data);
25
+    }
26
+}

+ 68
- 0
src/main/java/com/yunzhi/niucai/common/OSSUtils.java Näytä tiedosto

1
+package com.yunzhi.niucai.common;
2
+
3
+import com.aliyun.oss.OSSClient;
4
+import com.yunzhi.niucai.config.AliyunConfig;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Component;
7
+import org.springframework.web.multipart.MultipartFile;
8
+
9
+import java.io.*;
10
+
11
+@Component
12
+public class OSSUtils {
13
+
14
+    @Autowired
15
+    AliyunConfig aliyunConfig;
16
+
17
+    /**
18
+     * 上传文件
19
+     * @param mf 上传的文件
20
+     * @param toDirs 上传到的目录
21
+     * @return
22
+     */
23
+    public String putObject(MultipartFile mf, String ... toDirs) throws IOException {
24
+        String fName = formatFileName(mf.getOriginalFilename());
25
+        String preFix = String.valueOf(System.currentTimeMillis());
26
+        String toDir = "/";
27
+        if (null != toDirs && toDirs.length > 0) {
28
+            toDir = toDirs[0];
29
+        }
30
+        String nwFName = toDir + preFix + "-" + fName;
31
+        nwFName = StringUtils.trim(nwFName,"/");
32
+
33
+        return putFile(nwFName, new ByteArrayInputStream(mf.getBytes()));
34
+    }
35
+
36
+    public String putObject(File f, String ... toDirs) throws FileNotFoundException {
37
+        String fName = formatFileName(f.getName());
38
+        String preFix = String.valueOf(System.currentTimeMillis());
39
+        String toDir = "/";
40
+        if (null != toDirs && toDirs.length > 0) {
41
+            toDir = toDirs[0];
42
+        }
43
+        String nwFName = toDir + preFix + "-" + fName;
44
+        nwFName = StringUtils.trim(nwFName,"/");
45
+
46
+        InputStream inputStream = new FileInputStream(f);
47
+        return putFile(nwFName, inputStream);
48
+    }
49
+
50
+    private String formatFileName(String fName) {
51
+        return StringUtils.ifNull(fName, StringUtils.random(16) + ".png");
52
+    }
53
+
54
+    private String putFile(String fname, InputStream input) {
55
+        OSSClient ossClient = new OSSClient(aliyunConfig.getOss().getEndpoint(), aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret());
56
+        ossClient.putObject(aliyunConfig.getOss().getBucketName(), fname, input);
57
+        ossClient.shutdown();
58
+
59
+        // String url = ossClient.generatePresignedUrl(AliOSSUtils.oss.getBucketName(), fname, expiration).toString();
60
+
61
+        String preURL = aliyunConfig.getOss().getBucketURL();
62
+        if (!preURL.endsWith("/")) {
63
+            preURL += "/";
64
+        }
65
+
66
+        return preURL + fname.replaceAll("\\ ", "%20");
67
+    }
68
+}