瀏覽代碼

first commit

张延森 4 年之前
父節點
當前提交
2e83626e14
共有 3 個檔案被更改,包括 133 行新增12 行删除
  1. 39
    12
      pom.xml
  2. 26
    0
      src/main/java/com/yunzhi/niucai/common/ExcelUtils.java
  3. 68
    0
      src/main/java/com/yunzhi/niucai/common/OSSUtils.java

+ 39
- 12
pom.xml 查看文件

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

+ 26
- 0
src/main/java/com/yunzhi/niucai/common/ExcelUtils.java 查看文件

@@ -0,0 +1,26 @@
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 查看文件

@@ -0,0 +1,68 @@
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
+}